Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to rotate 2D object with mouse?

Discussion in 'Scripting' started by Deleted User, May 24, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hello, i'm making my first 2D game, i want to make a script that makes player arms with the rocket launcher aim to the mouse position from the rocket launcher, the problem is that i need it to get the position from rocket launcher and rotate arms with rocket launcher object. I couldn't figure it out, hope there is a way to do it.

    I had made a script that does it but it rotated the object to the wrong position so it wasn't very precise so i found much easier one but it doesn't rotate to the position i want, yet, hopefully. Here is the new script :
    Code (CSharp):
    1. //Gets the mouse position
    2. mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - camera.transform.position.z));
    3.  
    4. //Draws a ray from the pivot point from object to the mouse position
    5. Debug.DrawRay(transform.position, mousePosition - transform.position, Color.cyan);
    6.  
    7. //Rotates the object
    8. transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePosition - transform.position);


    So basically i want to rotate the player arms with rocket launcher to the mouse position from rocket launcher if that makes sense.
     
  2. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Try this, c# and js versions.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Thank you so much, that's exactly what i wanted!!!
     
    RavenOfCode likes this.