I know this question has been asked many times before, but I never seem to find a complete answer in the searches I’ve done (just “hints” or “clues” about general functions to call)
I am trying to move the main player/character to a target selected on the ground/floor plane. I’d like to have the player character do an animated walk over there at constant speed (ie. continue walking at that speed until destination reached.) I don’t want the rules of physics/collisions to disappear in the process, so this is why I am not simply lerping or moving transform to location… seems like I need to use CharacterController?
So, I have a few questions:
Do I need to go through the CharacterMotor to do this? Or can I bypass that script and access the CharacterController directly?
How? Obviously I’d really only be moving along two axes (not all 3; otherwise, the character would be going down into the floor at the selected point)
Is there anybody who has a completed script that does this? I haven’t found one, but it seems like a pretty common game task/feature…
Never mind, I found correct solution… I just didn’t realize that my Update scripting was preventing it from sort of running as a while loop. I just had to set a boolean flag to ignore the rest of the Update script until destination reached, and it worked fine:
void MoveTowardsTarget(Vector3 target) {
var offset = target - playerController.transform.position;
//Get the difference.
if (offset.magnitude > 0.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 * 5.0f;
//normalize it and account for movement speed.
playerController.Move (offset * Time.deltaTime);
//actually move the character.
} else {
moving = false;
timer = 2.5f; // reset timer
}
} // end MoveTowardsTarget