- Home /
Enemy walking through walls/terrain
Hi, I have a script for my two enemies in my game, and both of them will walk through walls and terrain regardless.
The Script is below:
using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour {
public Transform playerLocation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.LookAt(playerLocation);
transform.Translate(0, 0, 1 * Time.deltaTime * 10);
//GameObject.Find("GameObject");
}
void OnCollisionEnter(Collision collision){
if (collision.gameObject != null){
Destroy(gameObject);
}
}
}
What can I change to make it so they do not walk through walls/terrain
Answer by JoshDangIt · May 22, 2016 at 03:09 PM
You'll need to use rigidbodies to move rather than transform.translate. Translate essentially teleports an object to a new position, regardless of collision. Look up guides on Unity's physics system for more info.
Also, when you implement the rigidbody, you may have to remove your oncollisionenter function, as it will destroy the enemy on every collision even when it hits the ground.
Your answer
Follow this Question
Related Questions
Unity 3D Java - FPC Side Walking Through The Wall Problem 0 Answers
My Player only gets hurt on one side 0 Answers
Enemy HELP 1 Answer
FPS walking inside of a cylinder 1 Answer
Walking On Walls With Mouselook! 3 Answers