Skip to main content
deleted 884 characters in body
Source Link
Gnemlock
  • 5.3k
  • 5
  • 28
  • 59

After testing your code, the only conclusion I can come to is that you are not actually including a Rigidbody on your player. When we use GetComponent<t>(), we are referencing an existing component on the host GameObject.

Given your above code, on an object that contains a Rigidbody, the object moves quite clearly. I have included a recording, for reference.

With a valid rigidbody, the script easily allows me to move a cube around the screen.


Ensure your GameObject has a Rigidbody through code

You can add a simple tag to any script to force it to require a component. If said script is attached to a GameObject via the Inspector, the required component is added, too. You can do this with any Component; this is what it would look like if you forced your script to attach a Rigidbody:

[RequireComponent(typeof(Rigidbody))]
public class NewBehaviourScript1
{
    // ...
}

Ensure your movement is stable

As pointed out, in the comments, you have an issue with your moveSpeed. You probably want this value to be higher than 2, but as is, this is not as evident due to a different issue all together. Your not moving your player by 2 units; your moving your player by 2 units for ever FixedUpdate(). This may appear to be stable, but you can have varying results.

We can fix this with Time.fixedTimeDelta. Time.fixedTimeDelta scales the value to the time difference between each update. Moving an object by moveSpeed * Time.fixedTimeDelta ensures that the object will move moveSpeed units per second. If you are inside an Update() method, you should use Time.deltaTime.

This is a very important concept to understand. Read more at the Unity documentation for Time.deltaTime and Time.fixedDeltaTime.

After testing your code, the only conclusion I can come to is that you are not actually including a Rigidbody on your player. When we use GetComponent<t>(), we are referencing an existing component on the host GameObject.

Given your above code, on an object that contains a Rigidbody, the object moves quite clearly. I have included a recording, for reference.

With a valid rigidbody, the script easily allows me to move a cube around the screen.


Ensure your GameObject has a Rigidbody through code

You can add a simple tag to any script to force it to require a component. If said script is attached to a GameObject via the Inspector, the required component is added, too. You can do this with any Component; this is what it would look like if you forced your script to attach a Rigidbody:

[RequireComponent(typeof(Rigidbody))]
public class NewBehaviourScript1
{
    // ...
}

Ensure your movement is stable

As pointed out, in the comments, you have an issue with your moveSpeed. You probably want this value to be higher than 2, but as is, this is not as evident due to a different issue all together. Your not moving your player by 2 units; your moving your player by 2 units for ever FixedUpdate(). This may appear to be stable, but you can have varying results.

We can fix this with Time.fixedTimeDelta. Time.fixedTimeDelta scales the value to the time difference between each update. Moving an object by moveSpeed * Time.fixedTimeDelta ensures that the object will move moveSpeed units per second. If you are inside an Update() method, you should use Time.deltaTime.

This is a very important concept to understand. Read more at the Unity documentation for Time.deltaTime and Time.fixedDeltaTime.

After testing your code, the only conclusion I can come to is that you are not actually including a Rigidbody on your player. When we use GetComponent<t>(), we are referencing an existing component on the host GameObject.

Given your above code, on an object that contains a Rigidbody, the object moves quite clearly. I have included a recording, for reference.

With a valid rigidbody, the script easily allows me to move a cube around the screen.


Ensure your GameObject has a Rigidbody through code

You can add a simple tag to any script to force it to require a component. If said script is attached to a GameObject via the Inspector, the required component is added, too. You can do this with any Component; this is what it would look like if you forced your script to attach a Rigidbody:

[RequireComponent(typeof(Rigidbody))]
public class NewBehaviourScript1
{
    // ...
}
Source Link
Gnemlock
  • 5.3k
  • 5
  • 28
  • 59

After testing your code, the only conclusion I can come to is that you are not actually including a Rigidbody on your player. When we use GetComponent<t>(), we are referencing an existing component on the host GameObject.

Given your above code, on an object that contains a Rigidbody, the object moves quite clearly. I have included a recording, for reference.

With a valid rigidbody, the script easily allows me to move a cube around the screen.


Ensure your GameObject has a Rigidbody through code

You can add a simple tag to any script to force it to require a component. If said script is attached to a GameObject via the Inspector, the required component is added, too. You can do this with any Component; this is what it would look like if you forced your script to attach a Rigidbody:

[RequireComponent(typeof(Rigidbody))]
public class NewBehaviourScript1
{
    // ...
}

Ensure your movement is stable

As pointed out, in the comments, you have an issue with your moveSpeed. You probably want this value to be higher than 2, but as is, this is not as evident due to a different issue all together. Your not moving your player by 2 units; your moving your player by 2 units for ever FixedUpdate(). This may appear to be stable, but you can have varying results.

We can fix this with Time.fixedTimeDelta. Time.fixedTimeDelta scales the value to the time difference between each update. Moving an object by moveSpeed * Time.fixedTimeDelta ensures that the object will move moveSpeed units per second. If you are inside an Update() method, you should use Time.deltaTime.

This is a very important concept to understand. Read more at the Unity documentation for Time.deltaTime and Time.fixedDeltaTime.