首页 > 解决方案 > Unity3D 标准资产脚本无法正常运行

问题描述

我正在尝试使用 unity3D 标准资产完成一个项目。但是在导入资产时,脚本会产生错误——“文件中没有 MonoBehavior 脚本,或者它们的名称与文件名不匹配。” 和另一个错误 - “Assets\Standard Assets\Characters\RollerBall\Scripts\BallUserControl.cs(3,27): 错误 CS0234: 命名空间‘UnityStandardAssets’中不存在类型或命名空间名称‘CrossPlatformInput’(您是否缺少装配参考?)”。由于我是统一的新手,虽然我有先验知识但不是 C#,但我不理解这些错误。我还在下面给出了一个脚本来查找问题。

using System;
using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball
{
    public class Ball : MonoBehaviour
    {
        [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it.
        [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball.
        [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
        [SerializeField] private float m_JumpPower = 2; // The force added to the ball when it jumps.

        private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
        private Rigidbody m_Rigidbody;


        private void Start()
        {
            m_Rigidbody = GetComponent<Rigidbody>();
            // Set the maximum angular velocity.
            GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
        }


        public void Move(Vector3 moveDirection, bool jump)
        {
            // If using torque to rotate the ball...
            if (m_UseTorque)
            {
                // ... add torque around the axis defined by the move direction.
                m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
            }
            else
            {
                // Otherwise add force in the move direction.
                m_Rigidbody.AddForce(moveDirection*m_MovePower);
            }

            // If on the ground and jump is pressed...
            if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
            {
                // ... add force in upwards.
                m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
            }
        }
    }
}

标签: c#unity3d

解决方案


推荐阅读