Velocity change not working

Occasionally I run some code like this to change my velocity and move the enemy left and right. However sometimes this code doesn’t work. Using logs I can see it is definitely running this code and the velocity is changing to (4.00, 0.00, 0.00), however it inst moving the object at all. I’m sorry I’m new to unity 3D and have just been doing 2D for like 3 years lol.

I am doing it from an IEnumerator, as it an enemy AI script that runs every second or so. It is not kinematic as I need gravity and other forces. It does sometimes work just occasionally the code is running as I can see in the debug logs, but it still isn’t actually moving the object. The object is just a basic rectangle, with a slightly smaller box collider. I’ve made sure there is no friction on the physics material so it don’t think its that at least.

Also when it dosent move, I can see the velocity changes for a split section to the intedned value before instantly going back to 0.

the line of code:

rb.velocity = new Vector3(1 * speed, rb.velocity.y, 0);

Do you have more context? Is it a kinematic Rigidbody? Are you doing this from FixedUpdate?
[Edit, deleted some bad advice]
There’s a neat chart floating around on the forums somewhere that explains all the possibilities (couldn’t find it just now, sorry:), like how to configure stuff if you want to apply forces vs how to configure stuff if you want to set velocities directly.

I am doing it from an IEnumerator, as it an enemy AI script that runs every second or so. It is not kinematic as I need gravity and other forces. It does sometimes work just occasionally the code is running as I can see in the debug logs, but it still isn’t actually moving the object. The object is just a basic rectangle, with a slightly smaller box collider. I’ve made sure there is no friction on the physics material so it don’t think its that at least. I just cant figure out anything on why it wont move sometimes but does other times, I can see the velocity being set to the correct velocity in the debug logs but not in the inspector. Edit: I think I’ve found the chart but it dosnt help really, it seems like I have the correct settings toggled

Flowchart showing how to move your Rigidbody, for kinematic vs dynamic vs character controller, with AddForce, velocity or MovePosition :

Got it!

Also when it dosent move, I can see the velocity changes for a split section to the intedned value before instantly going back to 0.

Thanks I thought I found it but this looks way better !

1 Like

So it says “You set the desired velocity, but the physics system will then revise it every frame in the case of collision or other forces applied”. Could that be the problem? Sorry I’m not super-helpful, I’m all “apply forces”, I’ve not set the velocity of stuff myself in 3D, but I remember reading about this stuff so I linked. Hopefully someone else will be along to help out:)

I think I’ve found the issue, The gameobject seems to be getting caught on different sections of the floor gameobjects. I have no clue why it should be all flat but it seems to not be. Edit: All I had to do was change it to a capsule collider and it all fixed :slight_smile:

1 Like