視頻中的腳本最初有很多錯誤,我已經盡力修復代碼并將錯誤減少到盡可能少
我當前腳本中的錯誤是:Assets\PlayerMotor.cs(9,14): error CS1519: Invalid token '=' in class, struct, or interface member declaration
Assets\PlayerMotor.cs(9,32):錯誤 CS1001:需要識別符號
我的腳本是:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
private Vector3 velocity;
velocity = new Vector3(zero);
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
//Gets a movement vector
public void Move (Vector3 _velocity)
{
velocity = _velocity;
}
//Run every physics iteration
void FixedUpdate ()
{
PerformMovement();
}
//Perfrom movement based on velocity variable
void PerformMovement()
{
if (velocity != Vector3.zero){
rb.MovePosition(rb.position velocity * Time.fixedDeltaTime);
}
}
}
uj5u.com熱心網友回復:
您不能只在類體內撰寫代碼,例如:
velocity = new Vector3(zero);
。你能做的,并且可能讓你認為你能做到的,是在宣告時初始化變數:private Vector3 velocity = new Vector3(zero);
不過,這還有更多的問題。Vector3 沒有建構式,它只接受一個引數。
即使是這樣,您也必須將 Vector3 零宣告為靜態變數。
總而言之,在
Vector3.zero
需要的地方使用就行了,自己宣告也沒有意義。
uj5u.com熱心網友回復:
private Vector3 velocity;
velocity = new Vector3(zero);
這不起作用。你可以這樣寫
private Vector3 velocity = Vector3.zero;
或在以下方法中分配值Start
:
private Vector3 velocity;
void Start()
{
rb = GetComponent<Rigidbody>();
velocity = Vector3.zero;
}
也new Vector3(zero)
不起作用,因為您從未定義過任何東西zero
。這可能是一個錯字Vector3.zero
。另一種選擇是new Vector3(0, 0, 0)
.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/520010.html
標籤:C#unity3d
上一篇:Unity2D-如何為不同游戲物件中的每個角色健康設定實體化滑塊的值?
下一篇:使影像與填充中的段落文本對齊