Search Unity

Detect if other GO enter in line

Discussion in 'Scripting' started by PixelFireXY, Aug 17, 2012.

  1. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Hi, I want create a C# script for detect if a gameobjact enter in line of 2 gameobject.
    For example:
    I have GameObject A and GameObject B one at position 1 and B at position 3. GameObject C walk throught position 2, and print collision!

    I thought I might take the position of "A", then the position of "B" and then, so that if a GameObject with a collider enters the position between A and B sends a message to the console. What I thought, however, is that it takes too much memory to recalculate every frame, what do you think?

    There is it a simple grafic sample.
    Thanks in advance :)
    View attachment $example.bmp
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
  3. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Thanks it was exactly what i was looking :D
     
  4. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Do you know another script? But that detect just first object in ray? I find this link:
    http://answers.unity3d.com/questions/54154/raycast-hit-one-object-at-a-time.html
    But i have to work to this code:
    Code (csharp):
    1.  
    2.  
    3. private int layerMask = 1<<8;
    4.  
    5. void Update() {
    6. //START POINT OF INTEREST
    7.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    8.        
    9.         if (Physics.Raycast(transform.position, fwd, 10)) {
    10.            
    11.             if (Physics.Raycast(transform.position, fwd, 10, layerMask))
    12.                 print("There is player in front of the object!");
    13.             else {
    14.                 print("There is something in front of the object!");
    15.             }
    16.         }
    17. //END POINT OF INTEREST
    18. if(target != null) {
    19.             //se i tasti di input del tastierino numerico sono premuti
    20.             if(_rotateCameraPressed) {
    21.                 _x += Input.GetAxis("Rotate Camera Horizontal Buttons") * xSpeed * 0.02f;
    22.                 _y -= Input.GetAxis("Rotate Camera Vertical Buttons") * ySpeed * 0.02f;
    23.            
    24.             RotateCamera();
    25.                
    26.             }
    27.             //altrimenti se il tasto dx del mouse è premuto..
    28.             else if(_camButtonDown) {          
    29.            
    30.             _x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    31.             _y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    32.            
    33.             RotateCamera();
    34.         }
    35.         //altrimenti...
    36.         else {
    37.                
    38.             //_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
    39.             //_myTransform.LookAt(target);
    40.            
    41.            
    42.             // assegna alla variabile float la rotazione y del target(player)
    43.             float wantedRotationAngle = target.eulerAngles.y;
    44. //          prova = wantedRotationAngle;
    45.             float wantedHeight = target.position.y + height;
    46.                
    47.             float currentRotationAngle = _myTransform.eulerAngles.y;
    48.             float currentHeight = _myTransform.position.y;
    49.            
    50.             // Damp the rotation around the y-axis
    51.             currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    52.        
    53.             // Damp the height
    54.             currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    55.        
    56.             // Convert the angle into a rotation
    57.             Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
    58.            
    59.             // Set the position of the camera on the x-z plane to:
    60.             // distance meters behind the target
    61.             _myTransform.position = target.position;
    62.             _myTransform.position -= currentRotation * Vector3.forward * walkDistance;
    63.        
    64.             // Set the height of the camera
    65.             _myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);
    66.            
    67.             // Always look at the target
    68.             _myTransform.LookAt(target);
    69.            
    70.             }
    71.         }
    72.         else {
    73.             GameObject go = GameObject.FindGameObjectWithTag(cameraTagName);
    74.            
    75.             if(go == null)
    76.                 return;
    77.            
    78.             target = go.transform;
    79.         }
    80.     }
    81.    
    82.     private void RotateCamera() {
    83.         Quaternion rotation = Quaternion.Euler(_y, _x, 0);
    84.         Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
    85.  
    86.         _myTransform.rotation = rotation;
    87.         _myTransform.position = position;
    88.     }
    89.    
    90.     public void CameraSetup() {
    91.         _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
    92.         _myTransform.LookAt(target);
    93.     }
    94. }
    95. }
    96.  
    I attach this script on main camera and target is a simple cube(set layer 8), the camera follows cube and if object enter in ray of camera, print to console message.
    But the problem is that not work layer and not layer, if camera detect one, if i move another object in line, the script not detect change, but if i change public variable value called walkDistance > 10 it detect change. Can you help me? Thanks in advance :D

    ps ignore multiple line of comments :)
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to miss out the distance and layer mask parameters, so instead of
    Code (csharp):
    1. if (Physics.Raycast(transform.position, fwd, 10, layerMask)) { ... }
    ...you should use
    Code (csharp):
    1. if (Physics.Raycast(transform.position, fwd)) { ... }
     
  6. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Thanks, i try immediatly ^^
     
  7. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    I follow your istructions, but still to not working. I repost the code, please tell me what do you think :)
    Code (csharp):
    1.  
    2. Vector3 fwd = transform.TransformDirection(Vector3.forward);
    3.        
    4.             if (Physics.Raycast(transform.position, fwd, layerMask))
    5.                 print("There is player in front of the object!");
    6.             else if (Physics.Raycast(transform.position, fwd, 10))
    7.                 print("There is something in front of the object!");
    8.  
    It continue to print "There is player in front of the object!" :'(
     
    Last edited: Aug 24, 2012