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

Question How do I determine which side of a character the player has clicked their mouse?

Discussion in 'Scripting' started by polypixeluk, Oct 30, 2020.

  1. polypixeluk

    polypixeluk

    Joined:
    Jul 18, 2020
    Posts:
    53
    When the user clicks their mouse I need to know which side of the character's vision they have clicked on, allowing for the fact that the player will change direction.



    I store Input.mousePosition when the user clicks and know that it is a Vector2 starting at 0,0 at the bottom left of the screen. I also have the rotation and the position of the player however I've not been able to work out how to compare them to determine which side of the player the click is.
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Raycast to the world and get the raycast hit point. Use player.transform.TransformPoint(raycastHit.point). If the resulting vector's X is negative, it's left; positive, it's right.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If the player's forward vector is
    transform.forward,
    then the vector to the right is
    transform.right
    , and to the left is
    -transform.right


    You could check
    transform.position + transform.right
    and
    transform.position - transform.right
    and see which one is closer to the mouse click.
     
  4. Sparot

    Sparot

    Joined:
    Aug 23, 2020
    Posts:
    9
    You could use a dot product to determine the click side. Using a vector formed from the click position to the origin of your character, and the transform.right of your character, you dot those two together to get a singular floating point value. If the floating point value is positive, both vectors are pointing in the same direction. If the value is negative, the are pointing in opposite directions.


    Code (CSharp):
    1.  
    2. //Convert the mouse position into a ray that points outward from the camera at the click position.
    3. Ray tCameraRay = Camera.current.ScreenPointToRay(Input.mousePosition);
    4.  
    5. //Perform a raycast to get the position in space of the click. This only works if there is collision geometry to click on.
    6. RaycastHit tHit;
    7. if (Physics.Raycast(tCameraRay, out tHit, Mathf.Infinity, CollisionMask, QueryTriggerInteraction.Ignore))
    8. {
    9.     //Create a vector pointing towards the click point in the world from the character's position.
    10.     Vector3 tToClickFromCharacter = tHit.point - tCharacter.transform.position;
    11.  
    12.     //You need to normalize this vector. If you do not normalize, Dot will return the distance between the two points, not the direction.
    13.     tToClickFromCharacter.Normalize();
    14.  
    15.     //Dot the two vectors together to get a singular floating point value that indicates the direction of both vectors relative to each other. The order of the dot product does not matter.
    16.     if (Vector3.Dot(tCharacter.transform.right, tToClickFromCharacter) > 0)
    17.     {
    18.         //Your click was on the right side.
    19.     }
    20. }

    If a dot product returns a value greater than zero, that means both vectors point in roughly the same direction. In this example, the click was on the right side.

    If a dot product returns a value less than zero, then that means the vectors point in roughly opposite directions. In this example, the click was on the left side.

    If a dot product returns 0, that means both the vectors are at a 90 degree angle. In this example, click was directly in front of or behind the character.

    You can also dot against the forward and up vectors to determine if a click is in front of or behind the charactor, or if it is above or below. Dot products are incredibly powerful, and have a lot of use cases in game physics. I highly recommend getting familiar with them, even if you don't need them for this particular example.