1. Submit bugs to enter our 5.4 Beta Sweepstakes. Read more about it here.
  2. Unity 5.4 public beta is now available for all users. Grab the build and visit the 5.4 beta forum.
  3. Missing a module? More about the new installer in 5.3 here.
  4. 4.7.1 released for Xcode 7.3 support. Thread here.

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
    Messages:
    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.  
    3.  
    4. {
    5.     if(hit.gameObject.tag == "wall")
    6.  
    7.    
    8.     {
    9. //rotate in new direction
    10. transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
    11. FishAI.directionAvailable = false;
    12.     }  
    13.     else
    14.     {
    15.         FishAI.directionAvailable = true;      
    16.  
    17.     }
    18.    
    19. }
    20.  
    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
    Messages:
    8,769
    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.