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

[Solved] Switching camera from orthographic to perspective kills all mouse interaction.

Discussion in '2D' started by enfantTerrible, Jan 21, 2016.

  1. enfantTerrible

    enfantTerrible

    Joined:
    May 31, 2013
    Posts:
    4
    Hello forum,

    this is my 1st post here, so I may not be aware of some formal things and etiquettes and I also do not know if this question is asked or answered before. So feel free to give me feedback on any of the above.

    Here is my question:

    I have a scene with 2d objects (sprites) where I can interact with them via the mouse by clicking on them. This works great with the orthogonal camera. However, recently I added a few particle efffect and they look great if the camera is set to perspective by just choosing 'perspective' from the dropdown menu. No othere changes are made.

    But when I switch the camera from orthographic to perspective the mouse interaction does not work anymore. Any ideas ?

    Thank you
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Depends on how your interactions happen. Do you raycast? How do you get position to raycast from and to? If not, how else are you getting which sprite was clicked?
     
  3. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    When I switched from orthogonal to perspective, I had the same problem. Supplying a z coordinate (on top of x and y) solved the problem for me. In my code, the z position was calculated by taking the z position of the gameObject that was being clicked, minus Camera.main.transform.position.z
     
  4. enfantTerrible

    enfantTerrible

    Joined:
    May 31, 2013
    Posts:
    4
    Here is what I do:

    Code (CSharp):
    1.  
    2.       var screenPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    3.        SpriteRenderer sr = entry.transform.GetChild (1).GetComponent<SpriteRenderer> ();
    4.        screenPoint.z = sr.transform.position.z;
    5.        if (sr.bounds.Contains (screenPoint)) { doInteract(); }
    6.  
     
  5. enfantTerrible

    enfantTerrible

    Joined:
    May 31, 2013
    Posts:
    4

    Here is what I do:

    Code (CSharp):
    1.  
    2.       var screenPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    3.        SpriteRenderer sr = entry.transform.GetChild (1).GetComponent<SpriteRenderer> ();
    4.        screenPoint.z = sr.transform.position.z;
    5.        if (sr.bounds.Contains (screenPoint)) { doInteract(); }
    6.  
    So in other words I set the z position of the mouse to the z position of the sprite.
     
  6. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    Try screenPoint.z= sr.transform.position.z - Camera.main.transform.position.z;
     
  7. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    The problem: you get world coordinate of mouse in 3D space. You use ScreenToWorldPoint(Vector3) on Vector2(Input.mousePosition is listed as returning Vector3, but z component is always 0) which doesn't contain Z axis, and, thus, always returning position of camera in perspective camera due to how camera works(it returns something more closer to world position in 2D space, so no problem there due to how orthogonal camera works).
    What you need to do: Use Camera.ScreenPointToRay instead and raycast it till hitting plane your 2D is on. If your 2D is on XY that would be
    Code (CSharp):
    1. Camera cam = Camera.main;
    2. Ray ray = cam.ScreenPointToRay(Input.mousePoint);
    3. Vector3 result = ray.GetPoint((sr.transform.position.z - cam.transform.z )/ ray.direction.z);
    Notes: 1. didn't test code. 2. This isn't actual physics raycasting as there's no need. It's simple to calculate it by hand and that's much faster.
    What it does: 'draw' ray from camera towards where mouse looks. Find intersection of ray with plane with equation Z=sr.transform.position.z. (parallel to XY, Z is set to sr.transform.position.z) by calculating z difference. Then travel ray by found distance.
    Be careful with 0 division if you raycast parallel to XY;) That means you see 2D horizon and I don't think you want that anyway in 2D space, but who knows...
     
    Last edited: Jan 22, 2016
    dimzki and FlightOfOne like this.
  8. enfantTerrible

    enfantTerrible

    Joined:
    May 31, 2013
    Posts:
    4
    That worked !! I also understood what is going on and what was wrong. So thank you very very much :)

    Both of you helped me out. But since Teravisors version is more generic and works for any camera angle, zoom level etc. I will go for raycasting ;)