Another Rotate an object question

Hi Com,

So I’m relativly new to Unity and worked myself through some video tutorials and the unity beginners guide.
After that I tried to start coding my game idea but (obviously) I’m struggeling at the moment and would appreciate your help :slight_smile:

What I have so far:
-one script that detects the direction of a swipe (it’s from n3k’s camera rotation by swiping video)
-the other rotates the attached object in that direction

so my problem is that the object rotates around the local axis. So if I turn the object for 180° (on the Y) the rotation direction is inverted (on the X). That is what I want to fix. The object should rotate always in the Input direction.
And I have some trubble with the Lerp. But that is something I think I can fix myself… Maybe

The hierarchy:
SwipeManager is attached to SwipeManager and ObjectMotor is to Models with target “Models”(then problems with Lerp)
3024608--226055--ducumentation.JPG

public class ObjectMotor : MonoBehaviour
{
    public Transform target;

    private float smoothSpeed = 3f;


    private void FixedUpdate()
    {
        //transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, smoothSpeed * Time.deltaTime);
    }

    private void Update()
    {
        if (SwipeManager.Instance.IsSwiping(SwipeDirection.Left))
        {
            RotObj(Vector3.up * 90);
            Debug.Log("Left");
        }
        else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Right))
        {
            RotObj(Vector3.down * 90);
            Debug.Log("Right");
        }
        else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Up))
        {
            RotObj(Vector3.back * 90);
            Debug.Log("Up");
        }
        else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Down))
        {
            RotObj(Vector3.forward * 90);
            Debug.Log("Down");
        }
    }

    public void RotObj(Vector3 rotation)
    {
        target.rotation *= Quaternion.Euler(rotation);
    }
}
public enum SwipeDirection
{
    None = 0,
    Left = 1,
    Right = 2,
    Up = 4,
    Down = 8,
}

public class SwipeManager : MonoBehaviour
{
    private static SwipeManager instance;
    public static SwipeManager Instance { get{ return instance; } }

    public SwipeDirection Direction { set; get; }

    private Vector3 touchPosition;
    private float swipeResistanceX = 50.0f;
    private float swipeResistanceY = 50.0f;

    private void Start()
    {
        instance = this;
    }

    private void Update()
    {
        Direction = SwipeDirection.None;

        if (Input.GetMouseButtonDown(0))
        {
            touchPosition = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 deltaSwipe = touchPosition - Input.mousePosition;

            if (Mathf.Abs(deltaSwipe.x) > swipeResistanceX)
            {
                //Swipe on the X axis
                Direction |= (deltaSwipe.x < 0) ? SwipeDirection.Right : SwipeDirection.Left;
            }

            if (Mathf.Abs(deltaSwipe.y) > swipeResistanceY)
            {
                //Swipe on the Y axis
                Direction |= (deltaSwipe.y < 0) ? SwipeDirection.Up : SwipeDirection.Down;
            }
        }
    }

    public bool IsSwiping (SwipeDirection dir)
    {
        return (Direction & dir) == dir;
    }
}

I wanted to say a big THANK YOU to all the active community members here. I think it’s realy not self-evident that you are answering all of our nooby questions :wink:
So thank you for all your effort.

I’m not quite sure, but I think it might suffice to multiply your swipe direction in the other order. In other words, you have

  target.rotation *= Quaternion.Euler(rotation);

…which is equivalent to…

  target.rotation = target.rotation * Quaternion.Euler(rotation);

…so try this instead:

  target.rotation = Quaternion.Euler(rotation) * target.rotation;
1 Like

Thx. I didn’t thought it would be taht easy but ok. I can deal with it :slight_smile: