Enemies walk through some walls but not others with RayCast?

Hi All,

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

if (Physics.Raycast(transform.position, fwd, 2))
						{
           					if(Player.transform.rotation.y > 0)
							Player.transform.Rotate(0,40 * Time.deltaTime,0);
							if(Player.transform.rotation.y < 0)
							Player.transform.Rotate(0,-40 * Time.deltaTime,0);
						}
						else
						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

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.

This is how I do it C#

Ray wallray = new Ray(transform.position, transform.forward);
			if (Physics.Raycast (wallray, out hit, 10f))
			{
				if(hit.collider.tag=="wall")
				{
					//Turn around
				}
			}

Try drawing a debug ray so you can see if the rays are pointing in the correct direction and actually hitting something

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.

https://www.youtube.com/watch?v=-9YjR-Mszzc

Thanks

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!

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.

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.

if (Physics.Raycast(transform.position, fwd, 2))
      {
             if(Player.transform.rotation.y > 0) //** This never gets called
	     Player.transform.Rotate(0,40 * Time.deltaTime,0);
	     Debug.DrawRay(transform.position, fwd * 2, Color.blue); 
	     if(Player.transform.rotation.y < 0) //** This only gets called
	     Player.transform.Rotate(0,-40 * Time.deltaTime,0);
	     Debug.DrawRay(transform.position, fwd * 2, Color.white);
      }
	else
	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

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?

public Transform Player;
public Transform Target;

void Update () {
		
	if(Vector3.Distance(target.position, Player.position) > 20) //If player(zombie) is not in reach of target
		{
			{
				Player.animation.Play("Walking"); //play animation
		 		Debug.DrawLine(target.position, Player.position, Color.yellow); //Line to target
		 		Vector3 fwd = transform.TransformDirection(Vector3.forward); //Moves player forward
		 		Debug.DrawRay(transform.position, fwd * 2, Color.green); //Draws debug ray
        			if (Physics.Raycast(transform.position, fwd, 2)) //If something is in front of ray
						{
           					if(Player.transform.rotation.y > 0)
						{
							Player.transform.Rotate(0,40 * Time.deltaTime,0);
							Debug.DrawRay(transform.position, fwd * 2, Color.blue);
						}
							if(Player.transform.rotation.y < 0) //ONLY DOES THIS IF STATEMENT
						{
							Player.transform.Rotate(0,-40 * Time.deltaTime,0); //Rotate player
							Debug.DrawRay(transform.position, fwd * 2, Color.white); //Draw ray debug line
						}
						}
						else
						Player.transform.Translate(0,0,moveSpeed * Time.deltaTime);   //Move forward
}

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.

Is Player the empty game object, is it a parent of whatever this script is attached to.

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

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:

using UnityEngine;
using System.Collections;

public class zombie : MonoBehaviour
{
	public float moveSpeed = 4f;
	
	void Update ()
	{
		print (transform.rotation.y);
		Vector3 fwd = transform.TransformDirection(Vector3.forward); //Moves transform forward
		Debug.DrawRay(transform.position, fwd * 4, Color.green); //Draws debug ray
		if (Physics.Raycast(transform.position, fwd, 4)) //If something is in front of ray
		{
			if(transform.rotation.y > 0)
			{
				transform.Rotate(0,40f * Time.deltaTime,0);
				Debug.DrawRay(transform.position, fwd * 4, Color.blue);
			}
			if(transform.rotation.y < 0) //ONLY DOES THIS IF STATEMENT
			{
				transform.Rotate(0,-40f * Time.deltaTime,0); //Rotate transform
				Debug.DrawRay(transform.position, fwd * 4, Color.white); //Draw ray debug line
			}
		}
		transform.localPosition += transform.forward * moveSpeed * Time.deltaTime;   //Move forward
	}
}

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.

Thank you!!!
I finally got it working with a slight tweak :slight_smile:

Thanks for all your help!