i am trying to move character controller towards an object(target) but i want keep distance from the target.
i have used following code…in this code the character controller moves and reaches to the target.
how can i keep distance between character controller and target ??
void MoveTowardsTarget(Vector3 target) {
var cc = GetComponent<CharacterController>();
var offset = target - transform.position;
//Get the difference.
if(offset.magnitude > .1f) {
//If we're further away than .1 unit, move towards the target.
//The minimum allowable tolerance varies with the speed of the object and the framerate.
// 2 * tolerance must be >= moveSpeed / framerate or the object will jump right over the stop.
offset = offset.normalized * moveSpeed;
//normalize it and account for movement speed.
cc.Move(offset * Time.deltaTime);
//actually move the character.
}
}
Thanx.