我正在尝试使用unity5在线跟踪太空射击教程,我遇到了刚性问题。
我意识到刚体已被Component.GetComponent()取代,但我想创建一个变量,而不是全部输入。
我使用Component.GetComponent()得到了一大堆错误,并且不明白错误。
这是我的代码片段,我试图用clip来约束运动:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public float xMin, zMin, xMax, zMax;
void FixedUpdate(){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Component.GetComponent<Rigidbody>().velocity = movement*speed;
Component.GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp(Component.GetComponent<Rigidbody>().position.x, xMin, xMax),
0.0f,
Mathf.Clamp(Component.GetComponent<Rigidbody>().position.z, zMin, zMax)
);
}
}
以下是它给我的错误:
Finished updating scripts / assemblies
Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed.
Assets/Scripts/PlayerController.cs(14,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Assets/Scripts/PlayerController.cs(14,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Assets/Scripts/PlayerController.cs(18,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments
Assets/Scripts/PlayerController.cs(18,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float'
Assets/Scripts/PlayerController.cs(20,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
Assets/Scripts/PlayerController.cs(20,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments
Assets/Scripts/PlayerController.cs(20,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float'
Assets/Scripts/PlayerController.cs(21,18): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments
Assets/Scripts/PlayerController.cs(21,18): error CS1503: Argument `#1' cannot convert `object' expression to type `float'
Assets/Scripts/PlayerController.cs(16,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
我觉得我错过了一些重要而明显的东西,因为这并不是很多代码可以保证这么多错误。
答案 0 :(得分:1)
在使用非静态类函数之前,首先应该创建一个对象实例。
在你的情况下,很可能是附加了RigidBody组件的gameObject。这是代码示例:
gameObject.GetComponent<RigidBody>().velocity = movement * speed;
分别重做代码中的其他字符串。