- Home /
Weird collision error when using waypoint script...
I am trying to make a script so when the object the script is attached to collides with the target (waypoint end) the object will get destroyed. But when it is about to collide my object gets stopped by some sort of force field :o.
Here is my code:
var waypoint : Transform; var speed: float = 20;
function Update() { var target : Vector3 = waypoint.position; var moveDirection : Vector3 = target-transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){ velocity = Vector3.zero; }
else { velocity = moveDirection.normalized * speed; } rigidbody.velocity = velocity; }
function OnCollisionEnter(col : Collision) { if(col.gameObject == waypoint) { Destroy(gameObject);
} }
What is wrong? Thanks in advance.
NOTE: The collision error occurs only with the waypoint script.
Answer by Mike 3 · Jul 22, 2010 at 09:16 PM
Try change this:
if(moveDirection.magnitude < 1){
to this:
if(moveDirection.magnitude < 0.001){
Otherwise it'll be stopping 1m away from your object
It seems to be touching/colliding but the thing doesnt get destroyed.
also my building seems to sense collision but my soldier doesnt get destroyed....why is that?
should be col.transform ins$$anonymous$$d of col.gameObject (as waypoint is a transform)