Instantiated prefab object velocity is always zero

So I’m encountering an issue with an instantiated prefab object that I don’t encounter with objects already present in the scene when I hit “play”, as follows:

Vector2 dirToPlayer = (player.transform.position - transform.position).normalized;

rigidbody.velocity = dirToPlayer * speed;

print("Velocity SHOULD be - " + (dirToPlayer * speed)); // Always prints expected value
print("Velocity ACTUALLY is - " + rigidbody.velocity); // Matches value printed above for objects present in scene at launch, ALWAYS zero for object instantiated during runtime

I’ve double/triple/quadruple checked to make sure instantiated objects have all the correct values and settings. Any ideas as to why this may be occurring would be appreciated, thank you.

  1. Try removing or disabling all colliders of your prefab, it may be that it is instantiated in another object, this will break the physics simulation
  2. Set drag of the rigidbody to zero as it may have so much drag it instantly stops

Hope this helps.

Find what player.transform is (print its name) and what transform is

It’s probably the same thing. Where is this script? :slight_smile:

Print the two positions you’re subtracting! Get some answers fast by debugging!

This is all just basic debugging 101.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like