- Home /
move character controller smoothly to the object
character controller moves towards the object when that object is clicked but it does not go near that object , it stops according to the value set in moveSpeed variable.. please go through following code. i want to move it to the object , i have set its magnitude point and it stops at specific distance. but if the object is far then i have to click more then one times to bring the character to the magnitude point.
void MoveTowardsTarget(Vector3 target) {
var cc = GetComponent<CharacterController>();
var offset = target - transform.position;
//target = transform.position(0,0,-distance)*moveSpeed;
//Get the difference.
if(offset.magnitude > 6.0f) {
transform.position = targetPosition;
// motor.desiredMovementDirection = Vector3.zero;
//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;
//offset = offset.normalized * moveSpeed - distance + target.position;
//normalize it and account for movement speed.
//offset = Vector3.MoveTowards(offset, target , moveSpeed);
cc.Move(offset * Time.deltaTime);
//actually move the character.
print ("in function");
}
}
in short the character controller covers the distance according to the value set in moveSpeed variable..it does not reaches to the objects' magnitude point..!! how can i make it reach to the object in one click.??
Answer by Fornoreason1000 · Apr 24, 2014 at 11:09 AM
how do you call MoveTowardsTarget(target)? i'm guessing and hoping you've set it up to run for more than one frame. the way i see it, you character might be limiting is velocity(character motor.js has limiters in it, try setting max forward speed to 9999 see if it helps) or moveSpeed is messing up dues to some coding flaws. another possible reason is the switching mechanic you designed to make the character move is being turned off too early for whatever reason.
when you character stops does you Debug.Log statement also stop running? i'd check all the things i mentioned and probably debug some of the variables like move speed and offset to see where and why they are not doing what they are supposed to.
also just a helpful tip, use SqrMagnitude instead of magnitude. the reason being is that Cartesian distances uses SquareRoots and computers do not like Square root functions(they're slow). SqrMagnitude skips the Square rooting saving your Overhead a little. just remember to compare them with the square distance e.g for your case you want a distance of 6 so you would compare SqrMagnitude with 36.
not worked...i want to count distance between the character controller and the mouse click so using that distance i can set that distance as the moveSpeed.. i want to set it as moveSpeed because character controller covers distance according to the value set in moveSpeed. please help further if u can or let me know if u want to check rest of the code. thanks..
yeah i need to know the when $$anonymous$$oveTowardsTarget is called, its kinda of hard to tell whats calling it, you need to run long enough so it reaches the target in time.