Should I use Transform or Velocity?

So at first with Unity I was using transform to move objects, but then I started using velocity as most tutorials used it.

My question is what should I look for when deciding whether I should use Transform or Velocity to move an object? Aside from whether physics are involved of course.

Is one slower than the other? Is one better suited for a specific type of movement? In this case with moving enemies side to side, would either work okay?

Thank you for your time.

1 Like

Hi J,

This is a quote from the Unity - Scripting API documentation for Rigidbody.velocity, linked below;

“In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don’t set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity.”

…and this is a quote from the Unity - Scripting API for a transform;

“Every object in a scene has a Transform. It’s used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically.”

If you want your GameObject to be affected by physics then you’d add the Rigidbody component and, if you wanted to move it you could apply forces, the physics engine will then calculate where the object should be based on a number of factors and move it accordingly. If you don’t need to worry about physics, then you wouldn’t apply forces to it, you’d be setting the Is Kinematic property (assuming you still want the Rigidbody component for collisions etc), you’d then move the GameObject via its transform position.

Note, you could also use animation to move enemies side to side, this would be the same as setting the transform position yourself in code.

Hope this helps :slight_smile:


See also;

1 Like

Alright, thanks! I guess I’ll just stick to Transform for simple movements.

Animations tend to give me more trouble.

1 Like

You’re more than welcome :slight_smile:

You could think of these as different tools in your toolbox, and then aim to use the correct one for the job in question. If you wanted to move a cloud across the screen, perhaps add a Rigidbody and add a Wind source to the scene, let the physics engine handle that for you. Animation is great when you want to do more complex movements but without having to get your hands too dirty in code, in fact, if you haven’t already, there is some minor use of it in this section, just simple movements to give your enemies some life.

1 Like

Yes, I’ve already gotten to that part. It’s quite fun when you actually know what you’re doing.

I had some trouble at first because I was recording the position of the ships in world coordinates instead of coordinates relative to the parent position. :stuck_out_tongue:

1 Like

hehe, all part of the learning journey, glad you realised the issue and resolve it though.

You can get quite creative with this and obviously being able to apply it to then lots of enemies easily is a real bonus. With a few different animations you can make a very varied game. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms