Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Rotating Object at mouse position according to mouse movement direction

Discussion in 'Scripting' started by OZlachy, Jan 17, 2022.

  1. OZlachy

    OZlachy

    Joined:
    Oct 30, 2020
    Posts:
    4
    I am trying to rotate a player according to mouse movement. The player's position is set at the mouse position and as the mouse moves the player should rotate on the z-axis to face that direction. Something similar to this.

    Code (CSharp):
    1.    //Max value to add mouse value to, controls speed of rotation
    2.    float maxRotation = 2.5f;
    3.    //Adds the value of mouse movement
    4.    ver += Input.GetAxisRaw("Mouse Y");
    5.    hor += Input.GetAxisRaw("Mouse X");
    6.    //Clamps the value to stop rotating forever
    7.    ver = Mathf.Clamp(ver,-maxRotation,maxRotation);
    8.    hor = Mathf.Clamp(hor,-maxRotation,maxRotation);
    9.    //Rotates player
    10.    player.transform.rotation = Quaternion.Euler(0,0,((ver + hor) * (90/maxRotation)));
    11.    //Player stays at mouse pos
    12.    mousePos = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y,0);
    13.    player.transform.position = mousePos;
    14.  
    I have come up with this. The problem is the opposite axis has to equal zero for the direction to be correct. Otherwise, they don't add up to equal the correct direction.


    Cheers.
     
    Last edited: Jan 17, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,537
    This is a general scripting question so I'll move your post to the scripting forums here.

    FYI: The 2D forums are for specific 2D features and questions.
     
    OZlachy likes this.
  3. Daxior

    Daxior

    Joined:
    Feb 10, 2020
    Posts:
    8
    Since you want to stay turned front to mouse you can turn your character based on direction from your current player position to raycast hit place of mouse. Every time you move your mouse update that rotation and set a little stopping distance so character wont be rotating like crazy when it reaches mouse position.
     
    OZlachy likes this.
  4. OZlachy

    OZlachy

    Joined:
    Oct 30, 2020
    Posts:
    4
    Hey, thanks for the reply. Sorry, I'm not very familiar with raycasts so correct me if I am wrong but how would this work since the player is at the same position as the mouse?
     
  5. OZlachy

    OZlachy

    Joined:
    Oct 30, 2020
    Posts:
    4
    ty kind sir
     
  6. Daxior

    Daxior

    Joined:
    Feb 10, 2020
    Posts:
    8
    The point is to count difference between either current position and destination then perserve it as direction, or perserve old position and new one (depending on situation).

    To be clear:
    1. var characterPosition = player.parent.transform.position; //your character position
    2. You get raycastHit.point the ground/floor from casting a raycast
    3. var headingDirection = (raycastHit.point- characterPosition).normalized; //count difference between points
    4. var headingChange = Quaternion.FromToRotation(player.transform.forward, headingDirection); //count rotation
    5. player.transform.localRotation *= headingChange; //apply rotation to character
    Consider theese a scheme to work with :D
    also you can reffer to RaycastHit.point to see an example how to handle that
    https://docs.unity3d.com/ScriptReference/RaycastHit-point.html
     
    Last edited: Jan 17, 2022
    OZlachy likes this.
  7. OZlachy

    OZlachy

    Joined:
    Oct 30, 2020
    Posts:
    4
    Got it, thanks for the clarification :)