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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating a gameobject to mouse position

Discussion in 'Scripting' started by boylesg, Mar 24, 2016.

  1. boylesg

    boylesg

    Joined:
    Feb 18, 2016
    Posts:
    275
    I have a terrain with a shotgun construct siting in the middle.

    The camera has a crosshair object as a child and then the crosshair object as a shotgun model as its child.

    The crosshair object has a script that detects mouse movement and attempts to rotate the camera so that the it, the shotgun and the crosshair line up.

    But I cannot figure out how to get the crosshair to lock to the mouse position - at present I am just trying to get the rotation to work around the y axis and rotation of the gun up and down will come later once I figure this bit out.

    I have been getting the following problems with the rotation of the gun construct around the y axis:
    1) In the opposite direction to mouse movement
    2) Rotation of my gun to the left if the mouse is in the right half of the screen and I move it left - I want the crosshair to track the position of the mouse.

    I just can't figure out how to make this happen.

    I have tried transform.Rotate(Input.getAxis("Mouse X") * Mathf.radToDeg, ...) - this suffers from problem 2)

    I have tried ScreenToWorldPoint(...) and ScreenPointToRay(Input.mousePosition) but these suffers from problem 1)

    None them will result in the crosshair locking with the mouse pointer.

    So can anyone point me in the right direction?
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    heres some code i used for a camera to follow mouse position. you may be able to use it for your needs. I just copied and pasted it so you will have to re write it for your script. Basically it stores the mouse positions in the late update an compares them to the current mouse position. if they differ then the transform is rotated to the new direction.
    Code (CSharp):
    1. void LateUpdate()
    2.     {
    3.         mousex = Input.mousePosition.x;
    4.         mousey = Input.mousePosition.y;
    5.     }
    6.  
    7. void cameraRotating()
    8.     {
    9.         if(Input.mousePosition.x != mousex)
    10.         {
    11.             var playerRotY = (Input.mousePosition.x - mousex)*lookSpeed * Time.deltaTime;
    12.             transform.Rotate(0,playerRotY,0);
    13.  
    14.         }
    15.         if(Input.mousePosition.y != mousey)
    16.         {
    17.             var camRotX = (mousey - Input.mousePosition.y) * lookSpeed * Time.deltaTime;
    18.             var desiredRotX = cameraTrans.eulerAngles.x + camRotX;
    19.  
    20.             if(desiredRotX >= vertRotMin && desiredRotX <= VertRotMax)
    21.             {
    22.                 cameraTrans.Rotate(camRotX,0,0);
    23.  
    24.             }
    25.              
    26.  
    27.  
    28.         }
    29.     }
    there are vars in there for clamping max rotation angles "vertRotMax" etc. they arnt required.