Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check for collision with wall and move in a new direction

Discussion in 'Scripting' started by numeric, Jun 24, 2010.

  1. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    I made a post similar to this but it seems to be dead and being as I have new version of code I thought it would be best to explain in more detail.

    The problem is that my code doesnt seem to work - the cube moves through the wall somtimes and other times freezes after hitting a wall.

    Goal:
    To automate the movement of a cube within a enclosed area with four walls. If it collides with a wall the cube should move in a new random direction. It can only move forward/backward , and digonally.

    My approach:

    Check for collisions:
    Code (csharp):
    1.  
    2. function OnTriggerEnter (hit: Collider)
    3.  
    4.  
    5. {
    6.     if(hit.gameObject.tag == "wall")
    7.  
    8.    
    9.     {
    10. //rotate in new direction
    11. transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
    12. FishAI.directionAvailable = false;
    13.     }  
    14.     else
    15.     {
    16.         FishAI.directionAvailable = true;      
    17.  
    18.     }
    19.    
    20. }
    21.  
    Then generate a random direction and move in that direction:
    Code (csharp):
    1.  
    2. static var directionAvailable = false;
    3. static var randomNumber: int;
    4.  
    5.  
    6. function Awake()
    7. {
    8. //print ("no collision as of yet");
    9.    
    10. }  
    11.  
    12.  
    13.  
    14. function Update () {
    15.    
    16.  
    17. if (directionAvailable == false)
    18.     {
    19.         //randomNumber = (Random.Range(1, 4));
    20.         randomNumber = (randomNumber + Random.Range(1, 3)) % 3;
    21.         print (randomNumber);
    22.         directionAvailable = true;
    23.        
    24.     }
    25.    
    26.  
    27.    
    28.     //Use values obtained from above to move Fish
    29.     //forward
    30. if (randomNumber == 1)
    31.     {
    32.         transform.position.x += 0.04;
    33.        
    34.     }
    35.    
    36. // left diagonal
    37. if (randomNumber == 2)
    38.     {
    39.     transform.position.z += 0.04;
    40.     transform.position.x += 0.04;
    41.     }
    42.    
    43.  
    44. // Right Diagonal
    45.  
    46. if (randomNumber == 3)
    47.     {
    48.     transform.position.z += 0.04;
    49.     transform.position.x -= 0.04;
    50.     }
    51.    
    52.    
    53.    
    54.    
    55. }
    56.  
    It might be worth me starting from scratch?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Generally, triggers work much better as stationery objects than they do when they move. You will probably get better results by using a normal collider with your object and using OnCollisionEnter to tell you when it runs into a wall. Also, make sure the moving objects each have a rigidbody component with the Is Kinematic property switched on.