- Home /
Chasing Enemy travels through walls
This has appeared several times on unity but I have not been able to find a fixed piece of code. Could someone please provide an altered piece of code to prevent the chasing enemy from traveling through walls. This is the code I got from another question on UA:
#pragma strict
var target : Transform;
var moveSpeed = 240;
var rotationSpeed = 180;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
It was acquired from http://answers.unity3d.com/questions/26177/how-to-create-a-basic-follow-ai.html One comment mentioned about applying rigidbody forces but didn't show how to place it into the script. Thanks for the help!
hi,this is just a basic AI.It doesnt consider any obstacles or any other trigger conditions.You need something more advanced like a pathfinding logic to make ur situation to work.$$anonymous$$y suggestion is to start working using the A* path finding logic.
Lear what is A* path finding here
http://en.wikipedia.org/wiki/A*_search_algorithm
http://www.raywenderlich.com/4946/introduction-to-a-pathfinding
Answer by PiraLezz · Jul 11, 2014 at 12:12 PM
You have to add a "Character Controller" and for moving use this: var control : CharacterController;
//move the enemy
control.Move(Vector3(0,0,1));
Your answer
![](https://koobas.hobune.stream/wayback/20220613153756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollisionStay for seconds then destroy 0 Answers
Destroy a GameObject 2 Answers
Overlap Detection HELP!! 0 Answers
Change scene with trigger collision not working. 1 Answer
car moving sideways 1 Answer