C
C
Cobret2020-08-22 10:19:20
C++ / C#
Cobret, 2020-08-22 10:19:20

How to rotate an object in motion while maintaining its speed?

I'm still green in Unity, so I still don't understand how to rotate an object in motion while maintaining its speed and not giving a push in the other direction.

There is a sphere to which this script was attached:

public class MoveScript : MonoBehaviour
{
    public Rigidbody rb;
    public float force;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W)) 
            rb.AddForce(Vector3.forward * force);
    }
}

That is, when the W key is pressed, the sphere is pushed forward with the given force value. How to turn it on the move to the right / left without creating a new push?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2020-08-22
@XTerris

https://docs.unity3d.com/ScriptReference/Rigidbody...

G
GFX Data, 2020-08-22
@ShockWave2048

It is possible to transfer the render of an object to a rigid body sub-object and rotate it through a simple transform.

Z
zZaKko, 2020-08-22
@zZaKko

Rotate object after push:

public Vector3 need_rotate;
void Update()
    {
        if (Input.GetKey(KeyCode.W)) {
            rb.AddForce(Vector3.forward * force);
            transform.rotate(need_rotate);
}
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question