Rotate with mouse position

Hello, was wondering if anyone had any insight on how to rotate the ship relative to the pointers position? I have found Transform.LookAt() but cant figure out how to implement it without breaking everything. I’ve reversed all the rotating code back to its default.

    public void respondToRotateInput()
    {
        
        float rotationSpeed = rcsThrust * Time.deltaTime;

        if (Input.GetKey(KeyCode.A))
        {
            rigidBody.freezeRotation = true;
            transform.Rotate(Vector3.forward * rotationSpeed);
            rigidBody.freezeRotation = false;
        }

        else if (Input.GetKey(KeyCode.D))
        {
            rigidBody.freezeRotation = true;
            transform.Rotate(-Vector3.forward * rotationSpeed);
            rigidBody.freezeRotation = false;
        }
    }

Hi,

What exactly is your idea? What do you mean by “pointers position”?

1 Like

I’m trying to get the Y pivot of the rocket to aim at the mouse cursor. So that where ever the cursor is the object will rotate on the left or right Axis.

You could try the following:

Convert the mouse position to a World Space coordinate. You can do that with Camera.ScreenToWorldPoint.

Then you could use the Transform.LookAt method. The z-position of the converted mouse position is probably 0. Set the z-position of the returned Vector3 to the z-position of your rocket. Pass that Vector3 on to the LookAt method.


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms