I have followed and found countless posts and guides, yet this problem seems to never get fixed.
I have 8 objects that randomly choose an object which is set to targetFood to go to.
This is how I calculate the speed:
transform.position = Vector3.MoveTowards(transform.position, targetFood.transform.position, (Time.deltaTime * speed * GameObject.Find(“EnvironmentController”).GetComponent().time) / 60);
Basically, the last part is meant to make the objects go slower the more time they spend on the field (I have tried removing this in case that was the problem).
However for some reason, my objects sometimes move in a half circle to get to the object, and slow down drastically, then when they find a new object, it speeds up again to insane speeds sometimes.
I have tried to addforce on rigidbody, but then it doesn’t move at all.
Why does this happen?
The code looks OK, apart from the EnvironmentController.time, which we can’t look into. I assume counts down somehow? (Also, instead of searching for it every frame, it would be better for performance if you stored a reference to it on Start.)
Since you mentioned Rigidbodies: The commands you are using are intended for kinematic Rigidbodies or objects without Rigidbodies, so make sure that your object’s Rigidbodies are set to isKinematic = true. That also means you’ll get no collisions between the 8 objects. If that’s not intended behavior and the objects should be able to collide with each other it’s better to work with Rigidbody velocities instead.
Some more thoughts:
- Have your tried running the code in a clean test scene to see if any other part of your scene interferes with it?
- Also make sure your objects are not parented to other objects carrying non-kinematic Rigidbodies.