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
  4. Dismiss Notice

OnMouseDrag limit (resolved)

Discussion in 'Scripting' started by ernov, Mar 27, 2020.

  1. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    Hi
    I have a concern that I cannot solve, I try to make sure that the OnMouseDrag is limited on the x and z axes, I leave the y at 0
    Unfortunately it doesn't work
    Do you have an idea of the problem, thank you

    Code (CSharp):
    1.  
    2.         Vector3 worldPoint = Input.mousePosition;
    3.         worldPoint = Camera.main.ScreenToWorldPoint(worldPoint);
    4.         Vector3 diffPos = worldPoint - initMousePos;
    5.         diffPos.y = 0;
    6.         initMousePos = Input.mousePosition;
    7.  
    8.         initMousePos = Camera.main.ScreenToWorldPoint(initMousePos);
    9.  
    10.         obj.transform.position =
    11.             new Vector3(Mathf.Clamp(obj.transform.position.x + diffPos.x, -4.607f,3.77f),
    12.             obj.transform.position.y,
    13.             Mathf.Clamp(obj.transform.position.z + diffPos.z, -8f,5f));
    14.  
    15.    
     
    Last edited: Mar 27, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Code (CSharp):
    1. diffPos.y = 0;
    does nothing in the context of this script. It's not a reference type, so it doesn't have any effect just by changing it. Further in your script you only use the x and z value of the diffPos variable. You're not assigning the y value back to anything.
     
  3. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    Yes, I do want leave y at 0, which is the case
    I would just like to be able to move in x and z according to a given limit
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You don't get it...
    You are setting the vector3 variable diffPos.y = 0;
    You are never using that value anywhere else, so it doesn't do anything. Simply changing the value of the variable does nothing (since vector3 is a value type). You need to use it!

    Essentially you're doing this

    Code (CSharp):
    1. float y = 0;
    2. float x = someValue;
    3. float z = someValue;
    4.  
    5. //Do something with value x
    6. //Do something with value z
    7.  
    8. You never do anything with value y, so what should happen? You need to use value y somewhere to get the results you want, since you aren't using the entire vector3.
     
  5. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    Yes I understand that I do not use the y.
    Let's say I take it off, it's good in:
    Code (CSharp):
    1. obj.transform.position =
    2.             new Vector3(Mathf.Clamp(obj.transform.position.x + diffPos.x, -4.607f,3.77f),
    3.             obj.transform.position.y,
    4.             Mathf.Clamp(obj.transform.position.z + diffPos.z, -8f,5f));
    Now the goal is simply to be able to move my object over x and z with a limit
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Ok, so what part doesn't work? Is it not dragging or not dragging like you want it to?
     
    ernov likes this.
  7. ernov

    ernov

    Joined:
    Sep 17, 2014
    Posts:
    21
    I solved the problem, thanks anyway