Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  3. Unity 2017.2 is now released.
    Dismiss Notice
  4. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    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.  
    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
    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.