- Home /
C# CharacterController.SimpleMove Goes on Forever
Hi everyone, I'm having a bit of trouble with CharacterController.SimpleMove . It goes on Forever and I'm trying to come up with an if statement to stop it. But I'm having a hard time coming up with one. I want CharacterController.SimpleMove to stop once transform.position is at the same spot as targetPosition. I've tried if(transform.position != targetPosition) but that's not stopping it like I hoped. Any idea what's wrong with my script?
using UnityEngine;
using System.Collections;
public class moveOnMouseClick : MonoBehaviour {
public float speed = 5; // Determines how quickly object moves towards position
public Vector3 targetPosition; // current target pos
public CharacterController character;
public Vector3 dir;
void Start(){
//character = gameObject.GetComponent(CharacterController);
targetPosition = transform.position;
}
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse0)){
// create a logical horizontal plane at the player position:
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
// find point clicked in the plane (if any):
if (playerPlane.Raycast (ray, out hitdist)) {
// update targetPosition to the clicked point:
targetPosition = ray.GetPoint(hitdist);
dir = targetPosition - transform.position;
dir.y = 0; // keep only the horizontal direction
transform.rotation = Quaternion.LookRotation(dir);
}
}
// always try to move the character to targetPosition:
if(transform.position != targetPosition){
character.SimpleMove(dir); // move taking gravity into account
}
}
}
Answer by robertbu · Aug 10, 2013 at 06:19 AM
The chance that you will hit the target exactly is very small. That is, at each frame your character controller take a discrete step forward in the direction of the target. In one frame is will be before the target, in the next frame it will be beyond the target. One solution is to change your check to some threshold:
if ((transform.position - targetPosition).sqrMagnitude > some_small_threshold) {
character.SimpleMove(dir); // move taking gravity into account
)
What would be the closest amount I could get to the actual target? I gave the threshold 0.1f and that seems to be pretty close
if ((transform.position - targetPosition).sqr$$anonymous$$agnitude > 0.1f)
How close will depend on your your frame rate and speed. If you were not using Simple$$anonymous$$ove(), you could detect when you would step beyond the end point and just set it to the end point, but Simple$$anonymous$$ove does its own Time.deltaTime calculations.
Your answer
Follow this Question
Related Questions
C# FP_Camera not following Character Controller 0 Answers
C# Need Help converting from Rigidbody to Character Controller 0 Answers
C# Character Controller Collision If Statement 2 Answers
Converting C# Controller from WASD Controls to Click-Based 1 Answer
C# How to Access Character Controller's Properties without Declaring a Character Controller 1 Answer