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…
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.
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.
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.
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.
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