[英]How to make a player jump in unity using C#
我正在嘗試使播放器跳轉,但是我在rb.AddForce的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
}
}
}
AddForce是一種方法,而不是屬性。 方法必須被調用,您無需為其分配值。 嘗試
rb.AddForce(movement * speed);
除此之外,也許您應該更深入地了解編程基礎知識。 您可以將方法與屬性區分開來,因為方法名稱通常以動詞(“ AddForce”,“ CreateObject”等)開頭,而屬性名稱很好地描述了屬性(“ Color”,“ VerticalSpeed”,“ Name”,“。”)。 ..)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.