Search Unity

Rotate object to mouse position

Discussion in 'Scripting' started by Josebeli, Sep 11, 2018.

  1. Josebeli

    Josebeli

    Joined:
    Apr 29, 2017
    Posts:
    4
    I have this script, that already works
    I,m using leanFinger

    Code (CSharp):
    1.  
    2.         var c = Camera.main;
    3.         var center = rb.position;
    4.         var lastPos = _finger.GetWorldPosition(c.transform.position.y, c);
    5.         var lastDelta = Vector3.Distance(center, lastPos);
    6.  
    7.         float angle = Mathf.Atan2(lastPos.x, lastPos.z) * Mathf.Rad2Deg;
    8.         rb.rotation = Quaternion.Euler(new Vector3(0, angle - initialRotation, 0));
    The problem is that my object is rotating in absolute angles, so if it is already rotated, there is a unwanted rotation. I want it to take an initialRotation value, but i don't know how.

    Thanks
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do you want to be using LookAt instead then?
     
  3. Josebeli

    Josebeli

    Joined:
    Apr 29, 2017
    Posts:
    4
    With this approach, if the object is already rotated, that rotation will be ignored
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In that case I have no idea what you are wanting to achieve. :) Can you clarify?
     
  5. Josebeli

    Josebeli

    Joined:
    Apr 29, 2017
    Posts:
    4
    Lets say i have an object rotated 45° in Y axis (this value is not static). I want to rotate it in Y axis by the exact same degrees than _finger from its center. Later, i could just release it, and then rotate it again, like a gear
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
  7. Josebeli

    Josebeli

    Joined:
    Apr 29, 2017
    Posts:
    4
    That's exactly what i want, i didn't know that's how it's called this kind of drag. TYVM
     
    Doug_B likes this.