Question by
olliewe88 · Nov 05, 2018 at 03:06 PM ·
collisions
My enemies are going through walls and I have collisions on both of them
Hi All,
I have a zombie enemy and he keeps walking through the map to get to the player. I have collisions on the terrain and zombie. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zombie : MonoBehaviour
{
//The target player
public Transform Player;
//At what distance will the enemy walk towards the player?
//public float walkingDistance = 10.0f;
//In what time will the enemy complete the journey between its position and the players position
public float smoothTime = 10.0f;
//Vector3 used to store the velocity of the enemy
private Vector3 smoothVelocity = Vector3.zero;
public float speed = 50;
//Call every frame
void Update()
{
//Look at the player
transform.LookAt(Player);
//Calculate distance between player
float distance = Vector3.Distance(transform.position, Player.position);
//If the distance is smaller than the walkingDistance
//if (distance < walkingDistance)
//{
//Move the enemy towards the player with smoothdamp
transform.position = Vector3.SmoothDamp(transform.position, Player.position, ref smoothVelocity, smoothTime, speed);
//}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612181552im_/https://answers.unity.com/themes/thub/images/avi.jpg)