void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement
}
That right there is his code. I know I have to use GetComponent but how exactly?
My Code:
void fixedUpdate ()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>.velocity = movement;
}
All I get is this error “expression denotes a type’ where a variable’ value’ or method group’ was expected”
Make sure the script is attached to the same GameObject that has this Rigidbody. Then you could use a Rigidbody Field named rb (or anything else except “rigidbody” since this is already being used).
Rigidbody rb;
Then in an Awake Method you assign the reference to rb with GetComponent
void Awake()
{
rb = GetComponent<Rigidbody>();
}
Here is all of it combined
Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement;
}
ps. remember that letter case matters when using existing magic Methods like Awake Start Update and FixedUpdate.
void FixedUpdate()
{
// Correct
}
void fixedUpdate()
{
// Wrong
}