x


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.

more ▼

asked Jul 22 '10 at 09:04 PM

MikezNesh gravatar image

MikezNesh
843 64 74 93

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try change this:

if(moveDirection.magnitude < 1){

to this:

if(moveDirection.magnitude < 0.001){

Otherwise it'll be stopping 1m away from your object

more ▼

answered Jul 22 '10 at 09:16 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

It seems to be touching/colliding but the thing doesnt get destroyed.

Jul 22 '10 at 09:19 PM MikezNesh

also my building seems to sense collision but my soldier doesnt get destroyed....why is that?

Jul 22 '10 at 09:29 PM MikezNesh

should be col.transform instead of col.gameObject (as waypoint is a transform)

Jul 22 '10 at 10:01 PM Mike 3

Aahhh...nice catch Mike thanks so much! :D

Jul 22 '10 at 10:03 PM MikezNesh
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5085
x2499
x1951
x243
x170

asked: Jul 22 '10 at 09:04 PM

Seen: 1166 times

Last Updated: Jul 22 '10 at 09:04 PM