x


Finding Distances With Vector3

I'm trying to create a script, so that when the object 'runner' is 500m away, a rigidbody will be added. I've looked through the scipting manual, yet that produces an error, which I cannot solve. Where am I going wrong?

var runner : transform;
var closeDistance : float = 500;
    function Update (){
    if (runner) {
    var dist = Vector3.Distance(runner.position, transform.position);
    if (dist < closeDistance);
    if(!dist.gameObject.rigidbody){
       dist.gameObject.AddComponent(Rigidbody);
      }

    }
    }

Thanks.

more ▼

asked Apr 07 '11 at 04:44 PM

Muzz 1 gravatar image

Muzz 1
548 52 59 71

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

1 answer: sort voted first

I'm assuming you really want to be adding the Rigidbody to the runner object. You're attempting to add it to dist, which is a float value; epic fail.

You probably want something like this:

function Update (){
    if(runner){
        var dist = Vector3.Distance(runner.position, transform.position);
        if(dist < closeDistance && !runner.gameObject.rigidbody){
           runner.gameObject.AddComponent(Rigidbody);
        }
    }
}
more ▼

answered Apr 07 '11 at 04:53 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

(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:

x5077
x3460
x1278
x576
x354

asked: Apr 07 '11 at 04:44 PM

Seen: 596 times

Last Updated: Apr 07 '11 at 04:44 PM