I am trying to write code that is able to rotate object, based on camera position. So far I ended up with a code posted below. Moving mouse up and down (y input) caused to rotate object in y axis, instead of x. That's why I used cross product.

Achieved result is working not exactly as I expected. Rotation is different for every object, dependent on their pivot. I am not exactly sure how it fix it properly.

public class DragRotate : MonoBehaviour
    public Camera camera;
    public float rotationSpeed = 1;

    private void Update ()
        float x = Input.mousePosition.x * rotationSpeed;
        float y = Input.mousePosition.y * rotationSpeed;

        var rot = camera.transform.TransformDirection (new Vector3 (x, y));
        rot = Vector3.Cross(rot, camera.transform.forward);

        transform.rotation *= Quaternion.Euler (rot);

This script will be primarily used with one axis rotation, this feature is not implemented in code yet, but it's shown in the use case. According to attachment, I want to rotate selected cube (visible pivot is set to global). It's worth to say, that in this examples padlock has always same position and rotation, the only thing that changes is position and rotation of the camera:

  1. a) Swipe from bottom to top - negative rotation on X axis.b) Top to bottom positive on X axis. c) Other axes should be locked, but for example swipe from right bottom to left top should rotate like example a), but taking into consideration only y (vertical) input.
  2. This should behave exactly like example 1, but taking camera rotation into account. So 1a is 1b, 1b is 1a etc.
  3. Swipe from left to right should result in 1a rotation, swipe from right to left should result in 1b rotation. Bottom to top or top to bottom should have no effect at all.

Example cases of rotation

  • 1
    \$\begingroup\$ Can you diagram for us how you want this to behave? There are many different rotation schemes we can choose between, so the clearer we can understand your intentions, the more accurately we can target answers to achieve that aim. \$\endgroup\$
    – DMGregory
    Nov 18, 2020 at 16:13
  • \$\begingroup\$ @DMGregory I attached picture with different cases and added description. I hope you understand effect I want to achieve. \$\endgroup\$
    – Ardoos
    Nov 19, 2020 at 8:22

1 Answer 1


If I understand your problem correctly, all you need to do is rotate the object around its local X axis, regardless of how the mouse moves on the X-Y plane of the camera. What I would do is look at Transform.localRotation first and then, depending on whether the local X axis is aligned with the world X axis or is perpendicular to it, I would use the mouse position of either Y or X respectively and perform a local rotation accordingly, depending on the magnitude of Y or X.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .