The script in the video originally had a plethora of errors, and I've tried my best to fix the code and reduce the errors to as few as possible
The errors in my current script are: Assets\PlayerMotor.cs(9,14): error CS1519: Invalid token '=' in class, struct, or interface member declaration
Assets\PlayerMotor.cs(9,32): error CS1001: Identifier expected
My script is:
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);
}
}
}
CodePudding user response:
You can't just write code inside class body like:
velocity = new Vector3(zero);
. What you can do, and what probably made you thought you could do that, is to initialize the variable at declaration:private Vector3 velocity = new Vector3(zero);
Still, this has more issues.Vector3 doesn't have a constructor, that accepts only one parameter.
Even if it did, you would have to declare Vector3 zero as a static variable.
All in all, just use
Vector3.zero
where you need, there's no point in declaring it yourself.
CodePudding user response:
private Vector3 velocity;
velocity = new Vector3(zero);
This does not work. You can either write it like this
private Vector3 velocity = Vector3.zero;
or assign the value in a method like Start
:
private Vector3 velocity;
void Start()
{
rb = GetComponent<Rigidbody>();
velocity = Vector3.zero;
}
Also new Vector3(zero)
does not work, because you never defined whatever zero
is. It's probably a typo for Vector3.zero
. An alternative would be new Vector3(0, 0, 0)
.