0
\$\begingroup\$

I'm learning unity and i'm trying now make an inventory, i made a simple interface, and now i'm trying to make a item visualization. I look for some codes from web and i found this one:

using UnityEngine;

public class TurnObject : MonoBehaviour
{
    protected Vector3 posLastFame;
    public Camera UICam;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0)) {
            posLastFame = Input.mousePosition;
        }    

        if (Input.GetMouseButton(0)) {
            var delta = Input.mousePosition - posLastFame;
            posLastFame = Input.mousePosition;

            var axis = Quaternion.AngleAxis(-90f, Vector3.forward) * delta;
            transform.rotation = Quaternion.AngleAxis(delta.magnitude * 0.1f, axis) * transform.rotation;
        }
    }
}

I know some code, but I'm not good with geometrics. In this code when I move my mouse horizontal way, my object turn in the Y axis. And that's good. But when I move my mouse vertical the object move in the X axis instead of Z axis.

Is there a way in this code for change this? I really have difficulty to understanding geometrics in Unity.

\$\endgroup\$
1
\$\begingroup\$

This line:

var axis = Quaternion.AngleAxis(-90f, Vector3.forward) * delta;

Takes your mouse delta X & Y, and maps them to the world axes -Y and +X, respectively.

To change the mapping to use the Z axis instead of X, just use a different Quaternion, say...

var axis = Quaternion.LookRotation(Vector3.left, Vector3.forward) * delta;

This takes your X & Y to -Y and +Z respectively.

Just beware of creeping roll when you compound these rotations over many frames. For more intuitive controls, you might want to track yaw & pitch angle variables instead.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.