Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    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,392
    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,392
    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,392
  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.