Unity Community


Results 1 to 2 of 2

  1. Posts
    168

    Check for collision with wall and move in a new direction

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

    Then generate a random direction and move in that direction:
    Code:  
    1. static var directionAvailable = false;
    2. static var randomNumber: int;
    3.  
    4.  
    5. function Awake()
    6. {
    7. //print ("no collision as of yet");
    8.    
    9. }   
    10.  
    11.  
    12.  
    13. function Update () {
    14.    
    15.  
    16. if (directionAvailable == false)
    17.     {
    18.         //randomNumber = (Random.Range(1, 4));
    19.         randomNumber = (randomNumber + Random.Range(1, 3)) % 3;
    20.         print (randomNumber);
    21.         directionAvailable = true;
    22.        
    23.     }
    24.    
    25.  
    26.    
    27.     //Use values obtained from above to move Fish
    28.     //forward
    29. if (randomNumber == 1)
    30.     {
    31.         transform.position.x += 0.04;
    32.        
    33.     }
    34.    
    35. // left diagonal
    36. if (randomNumber == 2)
    37.     {
    38.     transform.position.z += 0.04;
    39.     transform.position.x += 0.04;
    40.     }
    41.    
    42.  
    43. // Right Diagonal
    44.  
    45. if (randomNumber == 3)
    46.     {
    47.     transform.position.z += 0.04;
    48.     transform.position.x -= 0.04;
    49.     }
    50.    
    51.    
    52.    
    53.    
    54. }

    It might be worth me starting from scratch?

  2. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    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.
    I'm wired to the world... that's how I... know... everything...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •