Search Unity

Enemies walk through some walls but not others with RayCast?

Discussion in 'Scripting' started by sim18, Jan 24, 2014.

  1. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Hi All,

    Like the post title, my enemies only walk through some walls and I can't put my finger on it.

    Code (csharp):
    1. if (Physics.Raycast(transform.position, fwd, 2))
    2.                         {
    3.                             if(Player.transform.rotation.y > 0)
    4.                             Player.transform.Rotate(0,40 * Time.deltaTime,0);
    5.                             if(Player.transform.rotation.y < 0)
    6.                             Player.transform.Rotate(0,-40 * Time.deltaTime,0);
    7.                         }
    8.                         else
    9.                         Player.transform.Translate(0,0,moveSpeed * Time.deltaTime);
    So if the enemy is going to collide with something they will rotate and carry on walking.
    I'm not sure why they don't turn with some walls, all colliders are setup exactly the same..

    Thank you
     
  2. Shinobi1507

    Shinobi1507

    Joined:
    Sep 8, 2010
    Posts:
    209
    Hey,

    This is indeed curious, is there any way you can show a video of what happens? Might give us some more info. And you double checked all the colliders? You could make a test scene that just has all duplicated colliders.
     
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    This is how I do it C#

    Code (csharp):
    1.  
    2. Ray wallray = new Ray(transform.position, transform.forward);
    3.             if (Physics.Raycast (wallray, out hit, 10f))
    4.             {
    5.                 if(hit.collider.tag=="wall")
    6.                 {
    7.                     //Turn around
    8.                 }
    9.             }
    10.  
    Try drawing a debug ray so you can see if the rays are pointing in the correct direction and actually hitting something
     
  4. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Thanks very much for the reply, I have made a quick YouTube video to show you.
    I have checked all of the colliders again. In the video I am using a test scene, all of the walls are exactly the same.



    Thanks
     
  5. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    I have added a debug ray that rotates as it's not just walls I want to collide with, I have added a video now, as you can see it hits the first two walls but not the left hand side wall. The zombie always goes through anything in the left hand side of my game!
     
  6. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    Change the color of the debug ray so you can tell when it's hitting something. That will let you know whether it's your colliders or your rotation code.
     
  7. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    This makes me think it's your rotation code. And, actually, looking at it you're using the x coordinate of the Quaternion which really isn't what you want to be using. Rotation.eulerAngles.x is probably what you want.

    Edit: Or y component actually.
     
  8. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Code (csharp):
    1.  
    2. if (Physics.Raycast(transform.position, fwd, 2))
    3.       {
    4.              if(Player.transform.rotation.y > 0) //** This never gets called
    5.          Player.transform.Rotate(0,40 * Time.deltaTime,0);
    6.          Debug.DrawRay(transform.position, fwd * 2, Color.blue);
    7.          if(Player.transform.rotation.y < 0) //** This only gets called
    8.          Player.transform.Rotate(0,-40 * Time.deltaTime,0);
    9.          Debug.DrawRay(transform.position, fwd * 2, Color.white);
    10.       }
    11.     else
    12.     Player.transform.Translate(0,0,moveSpeed * Time.deltaTime);
    Thank you, I can now see that only certain parts of this code get called
    Still have no idea why it's not working though
     
    Last edited: Jan 24, 2014
  9. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    How is the character setup and what is this script attached to and how is it calling Player

    In your video it looks like the zombie is made up from a few components.

    Have you tried getting this to work on a single object like a cube?
     
  10. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Code (csharp):
    1.  
    2. public Transform Player;
    3. public Transform Target;
    4.  
    5. void Update () {
    6.        
    7.     if(Vector3.Distance(target.position, Player.position) > 20) //If player(zombie) is not in reach of target
    8.         {
    9.             {
    10.                 Player.animation.Play("Walking"); //play animation
    11.                 Debug.DrawLine(target.position, Player.position, Color.yellow); //Line to target
    12.                 Vector3 fwd = transform.TransformDirection(Vector3.forward); //Moves player forward
    13.                 Debug.DrawRay(transform.position, fwd * 2, Color.green); //Draws debug ray
    14.                     if (Physics.Raycast(transform.position, fwd, 2)) //If something is in front of ray
    15.                         {
    16.                             if(Player.transform.rotation.y > 0)
    17.                         {
    18.                             Player.transform.Rotate(0,40 * Time.deltaTime,0);
    19.                             Debug.DrawRay(transform.position, fwd * 2, Color.blue);
    20.                         }
    21.                             if(Player.transform.rotation.y < 0) //ONLY DOES THIS IF STATEMENT
    22.                         {
    23.                             Player.transform.Rotate(0,-40 * Time.deltaTime,0); //Rotate player
    24.                             Debug.DrawRay(transform.position, fwd * 2, Color.white); //Draw ray debug line
    25.                         }
    26.                         }
    27.                         else
    28.                         Player.transform.Translate(0,0,moveSpeed * Time.deltaTime);   //Move forward
    29. }
    30.  
    This is actually attached to an empty game object that is a child of the actual Zombie. The colliders from the video have nothing to do with this as they detect if my car has hit them or not. There is more to the script but that only takes affect when the zombie is within distance to the target which in the video does not happen so I know that doesn't affect it.

    When I try to run it on a cube object the cube does not move at all.
     
  11. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Is Player the empty game object, is it a parent of whatever this script is attached to.
     
  12. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Sorry for the long wait.

    Player is my zombie character, not the empty game object.

    -ZombieCharacter
    -- Empty Game Object //This is the child of Zombie character this is where I attach the script to and in Unity I attach the 'ZombieCharacter' to the Player. Hope this makes sense
     
  13. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I think I understand how you have set it up.

    I put this script on a cube with a rigidbody and a box collider and it never went through any other collider:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class zombie : MonoBehaviour
    5. {
    6.     public float moveSpeed = 4f;
    7.    
    8.     void Update ()
    9.     {
    10.         print (transform.rotation.y);
    11.         Vector3 fwd = transform.TransformDirection(Vector3.forward); //Moves transform forward
    12.         Debug.DrawRay(transform.position, fwd * 4, Color.green); //Draws debug ray
    13.         if (Physics.Raycast(transform.position, fwd, 4)) //If something is in front of ray
    14.         {
    15.             if(transform.rotation.y > 0)
    16.             {
    17.                 transform.Rotate(0,40f * Time.deltaTime,0);
    18.                 Debug.DrawRay(transform.position, fwd * 4, Color.blue);
    19.             }
    20.             if(transform.rotation.y < 0) //ONLY DOES THIS IF STATEMENT
    21.             {
    22.                 transform.Rotate(0,-40f * Time.deltaTime,0); //Rotate transform
    23.                 Debug.DrawRay(transform.position, fwd * 4, Color.white); //Draw ray debug line
    24.             }
    25.         }
    26.         transform.localPosition += transform.forward * moveSpeed * Time.deltaTime;   //Move forward
    27.     }
    28. }
    I removed the target bit because I dont know how you are getting the target but the script above is almost the same as what you have.
     
    Last edited: Jan 24, 2014
  14. sim18

    sim18

    Joined:
    Oct 15, 2013
    Posts:
    36
    Thank you!!!
    I finally got it working with a slight tweak :)

    Thanks for all your help!
     
unityunity