I’m coding a script in C#. I’m building a tower defense and I’m having a huge issue here. My enemies spawn from a spawnpoint and goes all their way to where they are supposed to go.
When they get there, actually, I have to make them “stop”. So I’m using the “isKinematic” option coming with the rigidBody.
But my enemies stacks one atop the other. I’d need them to mass around, but stay put in y, only move in x.
If in the Editor you can just uncheck IsKinematic and just use the Rigidbody constraints, tick the position and rotation axes you want to restrict.
More specifically in code (c#), you can change the Rigidbody constraints with something like the following.
// To constrain the Y-axis position
private RigidbodyConstraints ConstrainYmovement;
ConstrainYmovement = RigidbodyConstraints.FreezePositionY;
//and when setting it to your Rigidbody.
transform.rigidbody.constraints = ConstrainYmovement;
// Set back to default (no constraints)
private RigidbodyConstraints DefaultContriants = RigidbodyConstraints.None;
transform.rigidbody.constraints = DefaultContriants;