- Home /
character controller that follow a target
hi at all. i am new in unity world ;) now i am working to behaviour of enemy in my game. i have to do a enemy (that have a character controller component) that follow a target. for do that i rotate enemy in direction on target with
characterController.LookAt(target.position) and this work great.
but after i have to move enemy in target direction. i tried to do something using function transormDirection but doesn't work following my code:
GameObject target; float speed; CharacterController character; Vector3 direction;
void Start () { speed = 0.2F; target=GameObject.Find("CubePath3"); character = GetComponent<CharacterController>(); }
// Update is called once per frame void Update () { direction = transform.TransformDirection(target.transform.position); transform.LookAt(target.transform.position); character.SimpleMove(direction*speed); }
somebody can help me? ;) thanks a lot ;)
Answer by superventure · Mar 23, 2011 at 06:12 PM
Try this in a regular Update function
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * speed);
transform.LookAt("player");
You don't need all that stuff in start with this by the way, just the speed var and set the player var with var player:GameObject;
Your answer
Follow this Question
Related Questions
HELP! i want to write to look at the target and give a force to follow 3 Answers
Camera focusing on an object specified in a script 1 Answer
How can I get this script to work? 1 Answer
How can I ground enemies and use target transform? 1 Answer
Coding equivalent of Draging an Object into the Inspector (Solved) 1 Answer