- Home /
Waypoint with Sphere Colliders and Character Controller Not Colliding
Hi, I'm creating a simple waypoint system for a game I am working on. I have a Character Controller that is currently a capsule traveling to one waypoint. When it collides with this waypoint, I want it to stop moving. The waypoints are empty game objects with a sphere collider attached. I'm not quite sure what to do here to get this to work. I've tried putting a rigidbody on the waypoints, but that didn't work.
Here is the Code I'm using:
// Our Destination Waypoint
var waypoint: Transform;
// Our Speed, Defaulted to 20
var speed : float = 20;
// The Magic Number that will make us grounded again
var magicNumber : float;
// Variable to Catch Our CharacterController
private var character : CharacterController;
// Have we collided with our waypoint?
private var collided : boolean = false;
function Start ()
{
character = GetComponent(CharacterController);
}
function Update ()
{
// Stop Moving!
if(collided)
return;
var target : Vector3 = waypoint.position;
// Keep WayPoint at Character's Height
target.y = transform.position.y;
var moveDirection : Vector3 = target - transform.position;
if(character.isGrounded)
moveDirection.y = magicNumber;
else
moveDirection += Physics.gravity * Time.deltaTime;
transform.LookAt(target);
character.Move(moveDirection.normalized * speed * Time.deltaTime);
}
function OnControllerColliderHit(collision : ControllerColliderHit)
{
if(collision.collider.name == waypoint.name)
{
// We've collided, so no more moving!
speed = 0;
collided = true;
}
}
Any help would be greatly appreciated. Thanks
Your answer
Follow this Question
Related Questions
Physics AddForce reduced when 3 objects are colliding 1 Answer
How to trigger different animations depending on where a gameobject collides? 1 Answer
Non-PhysX sphere - AABB intersections 0 Answers
OnCollisionEnter not working with CharacterController 1 Answer
OnCollisionEnter Push Object Problem 0 Answers