我正在尝试让我的播放器跳转,但我在使用rb.AddForce&#34时在fixedUpdate中出错:无法分配给AddForce,因为它是一个方法组"但是,在我添加跳跃速度和跳跃功能之前它是如何工作的?
代码:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed; //Creates Speed Variable
public Text countText; //Creates Count Text Variable
public Text winText; //Creates Win Text Variable
public float JumpSpeed;
private Rigidbody rb; //Creates Rigidbody Variable
private int count; //Creates Count Variable
void Start ()
{
rb = GetComponent<Rigidbody>(); //sets variable for Rigidbody
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate () //Controls
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
float moveForward = Input.GetAxis ("Forward");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce = (movement * speed);
if (Input.GetKeyUp (KeyCode.Space))
{
JumpSpeed = 5.0f;
Vector3 jump = new Vector3 (0.0f, moveForward, 0.0f);
}
}
void OnTriggerEnter(Collider other) //Collect Items
{
if (other.gameObject.CompareTag ("Pickup")) {
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText() //Updates Count Text
{
countText.text = "Count: " + count.ToString ();
if (count >= 8)
{
winText.text = "You Win!"; //Displays Win Text
}
}
}
答案 0 :(得分:1)
AddForce是一种方法,而不是属性。必须调用方法,不要为它们赋值。尝试
rb.AddForce(movement * speed);
除此之外,也许您应该深入了解编程基础知识。您可以区分方法和属性,因为方法名称通常以动词(“AddForce”,“CreateObject”等)开头,而属性名称则描述属性(“Color”,“VerticalSpeed”,“Name”,. ..)