Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Another Rotate an object question

Discussion in 'Scripting' started by TR33, Apr 7, 2017.

  1. TR33

    TR33

    Joined:
    Dec 27, 2016
    Posts:
    16
    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 :)

    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)
    ducumentation.JPG
    Code (CSharp):
    1. public class ObjectMotor : MonoBehaviour
    2. {
    3.     public Transform target;
    4.  
    5.     private float smoothSpeed = 3f;
    6.  
    7.  
    8.     private void FixedUpdate()
    9.     {
    10.         //transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, smoothSpeed * Time.deltaTime);
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         if (SwipeManager.Instance.IsSwiping(SwipeDirection.Left))
    16.         {
    17.             RotObj(Vector3.up * 90);
    18.             Debug.Log("Left");
    19.         }
    20.         else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Right))
    21.         {
    22.             RotObj(Vector3.down * 90);
    23.             Debug.Log("Right");
    24.         }
    25.         else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Up))
    26.         {
    27.             RotObj(Vector3.back * 90);
    28.             Debug.Log("Up");
    29.         }
    30.         else if (SwipeManager.Instance.IsSwiping(SwipeDirection.Down))
    31.         {
    32.             RotObj(Vector3.forward * 90);
    33.             Debug.Log("Down");
    34.         }
    35.     }
    36.  
    37.     public void RotObj(Vector3 rotation)
    38.     {
    39.         target.rotation *= Quaternion.Euler(rotation);
    40.     }
    41. }
    42.  
    Code (CSharp):
    1. public enum SwipeDirection
    2. {
    3.     None = 0,
    4.     Left = 1,
    5.     Right = 2,
    6.     Up = 4,
    7.     Down = 8,
    8. }
    9.  
    10. public class SwipeManager : MonoBehaviour
    11. {
    12.     private static SwipeManager instance;
    13.     public static SwipeManager Instance { get{ return instance; } }
    14.  
    15.     public SwipeDirection Direction { set; get; }
    16.  
    17.     private Vector3 touchPosition;
    18.     private float swipeResistanceX = 50.0f;
    19.     private float swipeResistanceY = 50.0f;
    20.  
    21.     private void Start()
    22.     {
    23.         instance = this;
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         Direction = SwipeDirection.None;
    29.  
    30.         if (Input.GetMouseButtonDown(0))
    31.         {
    32.             touchPosition = Input.mousePosition;
    33.         }
    34.         if (Input.GetMouseButtonUp(0))
    35.         {
    36.             Vector2 deltaSwipe = touchPosition - Input.mousePosition;
    37.  
    38.             if (Mathf.Abs(deltaSwipe.x) > swipeResistanceX)
    39.             {
    40.                 //Swipe on the X axis
    41.                 Direction |= (deltaSwipe.x < 0) ? SwipeDirection.Right : SwipeDirection.Left;
    42.             }
    43.  
    44.             if (Mathf.Abs(deltaSwipe.y) > swipeResistanceY)
    45.             {
    46.                 //Swipe on the Y axis
    47.                 Direction |= (deltaSwipe.y < 0) ? SwipeDirection.Up : SwipeDirection.Down;
    48.             }
    49.         }
    50.     }
    51.  
    52.     public bool IsSwiping (SwipeDirection dir)
    53.     {
    54.         return (Direction & dir) == dir;
    55.     }
    56. }
    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 ;)
    So thank you for all your effort.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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

    Code (csharp):
    1.   target.rotation *= Quaternion.Euler(rotation);
    ...which is equivalent to...

    Code (csharp):
    1.   target.rotation = target.rotation * Quaternion.Euler(rotation);
    ...so try this instead:

    Code (csharp):
    1.   target.rotation = Quaternion.Euler(rotation) * target.rotation;
     
    TR33 likes this.
  3. TR33

    TR33

    Joined:
    Dec 27, 2016
    Posts:
    16
    Thx. I didn't thought it would be taht easy but ok. I can deal with it :)