[CLOSED] Convert MovePosition and MoveRotation to rigidbody.velocity or AddForce / AddTorque

I use a script to move an object with MovePosition and MoveRotation. I want to try using a non-kinematic rigidbody so I need to have the same results as the script with a method that allows me to use gravity and physics. But I didn’t understand how to do it. When I use AddForce the rigid body takes on a speed that grows without stopping, when I use AddTorque, the rotation does not stop at the desired point but continues

public float speedRot = 0.5f;
private float speed = 3;

void Start()

rb = GetComponent<Rigidbody>();

void Update()

float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
movement = transform.forward * Vertical + transform.right * Horizontal;
movement.y = 0.0f;

direction = Camera.main.transform.forward;
direction.y = 0.00f;



private void FixedUpdate ()
    {

  rb.MovePosition(transform.position + movement * speed * Time.fixedDeltaTime);

Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
rb.rotation = Quaternion.LerpUnclamped(rb.rotation, targetRotation, speedRot * Time.fixedDeltaTime);

Something messed up while copy-pasting your code, it seems to be missing a lot of stuff (most notably { } braces).

AddForce and AddTorque are not the methods you want to use here - they’re one level too far removed. Basically:
AddForce affects velocity every frame; velocity affects position every frame.
AddTorque affects rotational velocity every frame; rotational velocity affect rotation every frame.

You’ll want to shift to setting .velocity and .angularVelocity. Like, you COULD use AddForce, but it’ll just involve a big pile of unnecessary math.

  rb.MovePosition(transform.position + movement * speed * Time.fixedDeltaTime);
// becomes
rb.velocity = movement * speed;

Angular velocity will be a little more difficult, just because rotations in a circle can make things hard to move in the correct direction. I would have to do some experimentation to work out a way to get it to rotate in the correct direction, but here’s something that might not rotate in the right direction:

rb.angularVelocity = targetRotation.eulerAngles - rb.rotation.eulerAngles;

You may be able to check the difference between those two Euler angles and add or subtract 360 to X/Y/Z until they’re all from -180 to 180, and that might work?

For the rotation, the object goes crazy, I tried to work a bit on but I can’t find a solution right now for your operation.

On movements, it is a simple equation and it works, only that to whatever object I apply it more than to move, the object rolls, even if I put it in a simple cube. While I would like it to move as it happens with MovePosition no rolling. If I wanted to use the script in the future to keep going on a person I imagine in this way that even his transform will not move but will roll, or am I wrong?

It is entirely possible that a Euler Angles based solution will just not work due to Euler angles being terrible. Might have to do some Quaternion.Inverse(a) * b sort of math?

You can lock the rotation on the rigidbody, but also this will stop happening when you get the rotation code ^ working.

In fact I blocked the rotation on the X and Z axes and now I have the movement that I just want that makes me think that if the object undergoes external forces it will not react realistically (for example if it is hit by a car it will not roll along those axes).
By the way, blocking the rotation in the X and Z axes, I just need to put “rb.rotation = Quaternion.LookRotation (direction, Vector3.up);” and the object turns where the camera points properly but, I can’t set the speed of rotation and it’s not a good solution I guess.

Can’t have it both ways - right now it’s just “reacting realistically” to the ground it’s being dragged over.

I didn’t think moving a rigidbody was so difficult, I don’t understand why they don’t put simpler methods with MoveRotation. By the way, using rb.velocity, now I notice that when I jump the object it slowly go back to the ground canceling the force of gravity, so I imagine that in order not to have this effect you have to use AddForce, which complicates things.
Please you have any suggestions ?

You could preserve the existing Y velocity - I’m guessing you don’t need the player to control that?

Vector3 vel = rigidbody.velocity;
vel.x = movement.x * speed;
vel.z = movement.z * speed;
rigidbody.velocity = vel;

this is currently my code. The object moves at the desired speed but using [rb.velocity = movement * speed] if it jump it remains glued to the ground. I can’t understand how I can fix things

private Vector3 movement;
private float speed = 3;
public bool isGrounded;

void Start()

rb = GetComponent<Rigidbody>();

void Update()
{
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
movement = transform.right * Horizontal + transform.forward * Vertical;
float origMagnitude = movement.magnitude;
movement.y = 0.0f;
movement = movement.normalized * origMagnitude;

if(isGrounded && Input.GetKeyDown(KeyCode.Space))
rb.AddForce((Vector3.up + movement) * JumpForce, ForceMode.Impulse);
      
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, rayDistance, layer)) isGrounded = true;
else isGrounded = false;
}

private void FixedUpdate ()
{       
     rb.velocity = movement * speed;
}