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

Detect if object is at a position and has rotation.

Discussion in 'Scripting' started by Hiilo, Aug 14, 2013.

  1. Hiilo

    Hiilo

    Joined:
    Jun 29, 2013
    Posts:
    4
    Hi. I'm hoping some one can point me to the right direction.
    Here's a quick description: In my scene, I have three(could be even more) different gameobjects and for each of these objects I have a different desired target position and rotation.
    A diagram:
    $IF-Position-01.png
    I want to achieve a way to detect if a object is at a certain position and has a certain rotation. Objects are moved by mouse drag and they have rigidbody's and colliders. It's also a 2D set up.

    I have done some research but got confused at what would be the best way to do it.
    Should I :
    1. Look in to overlap sphere? If so does each object need it's own?
    2. Make the target positions in to gameobjects, with colliders and somehow compare them against each other?
    3. Look in to 2D arrays as a way to give each gameobject it's target position and target rotation as Vector 3's ?
    4. Something completly different?
    I did some simple test's with only one object.
    Code (csharp):
    1. Vector3 targetPos=new Vector3(4.8f,4.4f,1.3f);//Got these numbers by Debug.Loging transform.position
    2. void OnMouseDrag()
    3.     {
    4.             Vector3 mousePos = myCam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,dist));
    5.             if(hit.collider.transform.gameObject.tag == "Player")
    6.             {
    7.               transform.position = mousePos;
    8.                 if(transform.position==targetPos){
    9.                 Debug.Log("It's working");
    10.                
    11.                 }
    12.                
    13.             }
    14.     }
    But had no luck. I also tried putting the if transform.position==targetPos inside Update function, but it wasn't any better. If I would set the transform.position = targetPos (inside Start function) then I would get the "It's working" log. I guess the drag part is way to jittery to mach those floats.

    So what would be the way to go?
    If possible then I would like to avoid physics.

    Hope it made some sense.
    Any help much appreciated.
     
  2. Avalion

    Avalion

    Joined:
    May 2, 2012
    Posts:
    34
    Why don't you try to approximate position and rotation ?

    If you want to code a Tangram game, I think you'd better to work with Vector3.Distance.

    By this way, you will be able to manage precision of recognition, having two floats, and consider an object is in the right place when Vector3.Distance(transform.position, targetPos) < precisionDistance Vector3.Distance(transform.eulerAngles, targetRot) < precisionRotation.

    After that, I rather you to make targetPos a public variable which is editable in Unity.

    Sorry for my English, Ask if you don't understand something.
     
  3. Hiilo

    Hiilo

    Joined:
    Jun 29, 2013
    Posts:
    4
    Hey. Thanks for the reply. I did a quick test with position values and it seems to work :)
    Heres how I did it
    Code (csharp):
    1. float acceptableRange=1.0f;
    2. Vector3 targetPos=new Vector3(4.8f,4.4f,1.3f);
    3. void OnMouseDrag()
    4.     {
    5.            
    6.             Vector3 mousePos = myCam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,dist));
    7.             if(hit.collider.transform.gameObject.tag == "Player")
    8.             {
    9.               transform.position = mousePos;
    10.                 float distDiff = Vector3.Distance(targetPos, transform.position);
    11.                 Debug.Log("Distance difference"+distDiff);
    12.        
    13.                 if(distDiff < acceptableRange)
    14.                 {
    15.                     Debug.Log("Object in range");
    16.                     transform.renderer.material.color=Color.red;
    17.                 }
    18.                
    19.             }
    20.        
    21.         }
    Actually I had in mind to have some sort of snapping feature. This would be super easy to do now.
    I guess the next step is to figure out how to assign different targetPositions and rotations for each object. Any suggestions, pointers?

    Anyway big thank you for the help.