- Home /
Character Controller moves forever after colliding with object
I am working on a click-to-move character controller. I'm trying to learn the concepts behind it. So far, everything I have works perfectly, save for one thing. In my scene, I have three cube gameObjects on the scene. One Cube has my script connected to it, the other two are there just as obstacles.
The problem I have is, at some times, if my cube I'm controlling collides with another cube, when it finally gets around it, it at times continues travelling forever.
Please forgive me for not knowing how to format the code to highlight syntax and such.
Here is my code I have:
var speed : int = 3;
var targetPosition : Vector3;
var travelDistance : float = 0.0;
var curSpeed : float;
var defaultSpeed : int = 3;
function Start () {
targetPosition = transform.position;
}
@script RequireComponent(CharacterController)
function Update () {
var character : CharacterController = GetComponent("CharacterController");
travelDistance = Vector3.Distance(transform.position, targetPosition);
if(travelDistance < .75){
speed = 0;
}
else{
speed = defaultSpeed;
}
//On Mouse left Click
if(Input.GetMouseButton(0)){
//Create a plane for the RayCast to hit
var playerPlane : Plane = new Plane(Vector3.up, transform.position);
var myRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//variable for Raycast hit distance
var hitDist : float = 0.0;
//check if the Raycast hits the plane
if(playerPlane.Raycast(myRay, hitDist)){
//create the target Vector where the plane hit
targetPosition = myRay.GetPoint(hitDist);
transform.LookAt(targetPosition);
}
}
if(travelDistance > .75){
curSpeed = speed * Time.deltaTime;
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
character.Move(forward * curSpeed);
}
Debug.Log(travelDistance);
}
I don't know why it's doing that. If anyone can help me understand why sometimes the cube will just travel forever after colliding with another, I'd really appreciate it.
Answer by car104 · May 01, 2013 at 02:07 PM
Just in case anyone ever comes across this, because I hate finding resolved issues with no answer provided. I have solved this problem. The issue was, when the character(cube) collided with another object, it would offset itself in one direction or the other to get around the object. As my code shows, the movement is controlled by finding a point the ray hit, turn the object to face that point and then move forward until it reaches that point, or within .5m of it. If the character's position is offset more than .5m after colliding with an object, it never gets within .5m distance of the destination, and thus continues travelling forever. I only had the object turn to look at the point the ray hit on mouse click, and thus it didn't update as it moved itself. To remedy this, I told the character to rotate toward the destination point every update.
Now if the position of the character gets offset by a collision, it'll rotate toward the point and continue moving toward it and finally stopping.
Your answer
Follow this Question
Related Questions
CharacterController unexpectedly moving up 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gradual Speed Increase with Variable Input 0 Answers
How to move to specific point with character controller 1 Answer
CharacterController NullReferenceException dispite attached to object 1 Answer