Hi. I have a rigidbody that I would like to move along a plane at a constant velocity along its local x axis. Using AddRelativeForce this would be as simple as AddRelativeForce(Vector3.right). However, adding a force means that the object will accelerate constantly while the force is being applied. What I would like to happen is for the object to immediately move at a predetermined velocity when the move bool is on and then immediately stop when the move bool is off. I tried experimenting with different friction settings to get this approximate behavior with AddRelativeForce but the engine seems like it’s too finicky for the level of control I need. I was hoping I could use MovePosition to achieve this effect instead. My code looks like this:
void FixedUpdate () {
if(conveyorOn && !palletStopped)
{
Vector3 coords = rigidbody.position;
float rot = transform.localEulerAngles.y;
Vector3 direction = new Vector3 (Mathf.Cos (rot), 0f, Mathf.Sin (rot));
rigidbody.MovePosition (coords + direction * speed * Time.deltaTime);
}
}
I assumed this would work. It exhibits the right behavior when the object’s axes are aligned with the world axes but changing the y rotation of the object in the inspector to anything but zero exhibits strange behavior: it never moves in the direction I would expect, although it’s often close. I’ve tried both transform.eulerAngles and transform.localEulerAngles with the same result. Does anyone know what I’m doing wrong here? I just want the object to move along its own x axis.