Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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,404
    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,404
    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,404
  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.