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

Cursor goes throgh plane

Discussion in 'Physics' started by filipetakanap, Nov 24, 2021.

  1. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I'll try to explain the best i can. I have this square plane which is ment to move objects around. But when i move something down it goes trough the plane to like x-100000 y -100000

    Code (CSharp):
    1.  void OnMouseDown()
    2.     {
    3.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    4.      
    5.         x = Camera.main.transform.rotation.x;
    6.         y = Camera.main.transform.rotation.y;
    7.         z = Camera.main.transform.rotation.z;
    8.        
    9.  
    10.      
    11.  
    12.     }
    13.     private Vector3 GetMouseAsWorldPoint()
    14.     {
    15.         // Pixel coordinates of mouse (x,y)
    16.         Vector3 mousePoint = Input.mousePosition;
    17.         // z coordinate of game object on screen
    18.         mousePoint.z = mZCoord;
    19.         return Camera.main.ScreenToWorldPoint(mousePoint);
    20.     }
    21.  
    22.  
    23.  
    24.     void OnMouseDrag()
    25.     {
    26.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.         RaycastHit hit;
    28.  
    29.         if (!Physics.Raycast(ray, out hit, 100))
    30.         {  
    31.             position = (transform.position = GetMouseAsWorldPoint());
    32.  
    33.  
    34.             if ((position.x < -4.272f && position.x > -5.272f) && (position.z > -5.21 && position.z < -4.21))
    35.             {
    36.                 gameObject.transform.position = new Vector3(-4.572f, 0.544f, -4.645f);
    37.  
    38.             }
    39.             else if ((position.x < -3.272f && position.x > -4.272f) && (position.z > -5.21 && position.z < -4.21))
    40.             {
    41.              (...)
    42.         }
    43.         else
    44.         {
    45.             Vector3 mousePos = Input.mousePosition;
    46.             gameObject.transform.position = new Vector3 (mousePos.x, 4.43f, mousePos.z);
    47.         }
    48.     }
    The final goal is for the user to snap objects around the plane.

    Here is an example:
     
    Last edited: Nov 24, 2021
  2. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    i think for a lack of better solution, i'll make the editing mode with the camera looking downwards, seems to work fine like that..
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    So do you have a problem with the Raycast call because that's the only part of this post that relates to physics. If not then this is really a post for the scripting forums.
     
  4. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Sorry, i tought it was more related to physics
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    It is if you're asking about the raycast call as I said. If it's all the other stuff, that's general scripting. The Transform system isn't the physics system.

    It's often why devs don't reply because it's in a dedicated forum and isn't related to the forum subject.