Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question LookAt(ScreenToWorldPoint) not working consistently.

Discussion in 'Scripting' started by EOMedvis, Mar 17, 2023.

  1. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    91
    I'm having trouble getting my mouse to look at where the cursor is pointing, even though the same function works fine for touch points when using to zoom in to where I'm pointing.

    Here's my touch screen code. This is a pinch to zoom function, basically I'm getting the midpoint between the 2 touch points, transforming it to a world position, then pointing the camera at it. This one works as intended. The commented out parts have to do with the zoom, so not the parts in question.

    Code (CSharp):
    1.  
    2.         Touch touch1 = Input.GetTouch(0);
    3.         Touch touch2 = Input.GetTouch(1);
    4.         Vector2 t1Previous = touch1.position - touch1.deltaPosition;
    5.         Vector2 t2Previous = touch2.position - touch2.deltaPosition;
    6.        Vector2 currentMidPoint = (touch1.position + touch2.position) / 2;
    7.         Vector3 lookAtPos = nonARCamera.ScreenToWorldPoint(currentMidPoint);
    8.         //float oldTouchDistance = Vector2.Distance(t1Previous, t2Previous);
    9.         //float currentTouchDistance = Vector2.Distance(touch1.position, touch2.position);
    10.         //float deltaDistance = oldTouchDistance - currentTouchDistance;      
    11.         //nonARCamera.fieldOfView += PinchAmountCalculator(deltaDistance);
    12.         //nonARCamera.fieldOfView = Mathf.Clamp(nonARCamera.fieldOfView, zoomLimit[0], zoomLimit[1]);
    13.         nonARCamera.gameObject.transform.LookAt(lookAtPos);
    14.  
    However, when I tried to do the same with the mouse pointer, it no longer works. If I use a Vector2 for the mouse position, nothing happens. If I use the normal mousePosition as a Vector3, it just looks off in the wrong position.

    (This is with a Vector2)
    Code (CSharp):
    1. if (Input.mouseScrollDelta.y != 0)
    2.         {          
    3.             Vector2 zoomPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);        
    4.             Vector3 lookPos = Camera.main.ScreenToWorldPoint(zoomPos);
    5.             cameraTilt.LookAt(lookPos);      
    6.         }

    (This is with the vector3, with some depth for the mouse position)
    Code (CSharp):
    1. if (Input.mouseScrollDelta.y != 0)
    2.         {          
    3.             Vector3 zoomPos = Input.mousePosition;
    4.             zoomPos.z = 5f;
    5.             Vector3 lookPos = Camera.main.ScreenToWorldPoint(zoomPos);
    6.             cameraTilt.LookAt(lookPos);      
    7.         }
    The vector 3 just makes the camera look down progressively. All of the objects are pointing the right directions in the game scene. So I don't think its that. What I'm not understanding is why I can use a Vector2 with touch and have the camera look at the right position, but I can't do the same with the mouse position.

    Am I overlooking something obvious? Thanks in advanced. :)
     
  2. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    328
    Only a guess, I don't know if it will work.

    Maybe try:
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    And then set the camera.forward to ray.direction.

    Also check cameraTilt is actually the Camera.main (don't know if you have more cameras)
     
    EOMedvis likes this.
  3. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    91
    So the problem is because the Z component for the mouse position was set too high. I'm not sure how the functions work in the background, but having a high z-component causes the movement to be much more drastic. Lowering it from 5f to 0.1f seems to have fixed the issue.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,288
    You do understand what the Z means to Camera.ScreenToWorldPoint(), correct?

    It is as important (if not moreso) than the X and Y portions of that Vector3 argument.

    Hurry to the docs if you have any doubt about what it does.