1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    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
    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.