- Home /
target.gameObject Position
my controller go to target position ?
var controller:CharacterController;
var target:Transform;
var speed:float=0.2;
var direction:Vector3=Vector3.zero;
function Update () {
direction=target.transform.position;
direction=transform.TransformDirection(direction);
direction*=speed;
controller.Move(direction*Time.deltaTime);
}
Answer by shaderop · Sep 28, 2012 at 12:51 AM
If you mean that you want to move your controller towards the object located at the `target` transform, then you need to find the vector from your controller's position to the target's position. Like so:
direction = (target.position - transform.position).normalized;
controller.Move(direction * Time.deltaTime * speed);
Sorry, my mistake. It was supposed to be "normalized" ins$$anonymous$$d of "magnitude." And what "normalized" does is give you a vector that has a length of one, so that the distance between the target and the object wouldn't affect the speed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081559im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast not working correct 0 Answers
Trigger animation cause the player to move another postion. 1 Answer
Negative Positions Breaks Raycasts 1 Answer
XR Controller (XR Ray Interactor) rotation and position offset? 5 Answers
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers