x


moving rigidbody to a moving target destination at a set speed

So I'm working on my rocket launcher in my 3rd person shooter. I'm trying to get it so the rocket launcher fires the rocket and it flies at my target gameobject (which is a GameObject that is constantly moving, to a position based off a raycast from the center of my 3rd person camera).

Here's how I currently have my rocket launcher script:

var projectile : Rigidbody;
var initialSpeed = 20.0;
var target : Transform;

function Update(){

    if(Input.GetButton("Fire1")){

        //create a new projectile, use the same position and rotation as the launcher
        var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);

        // First you get the vector from the current position
        // to the destination, and normalize it so you
        // get the direction:
        var direction = (target.position - transform.position).normalized;

        // Then add the direction * the speed to the current position:
        instantiatedProjectile.velocity = direction * initialSpeed; 

        //Ignore collisions between the missile and the character controller
        Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

    }
}

My problem is, the initialSpeed variable doesn't actually effect the speed of the rocket at all. As far as I can tell, it doesn't do anything. If I take away the .normalized on the direction variable, the behavior of the rocket is really strange: It will fly slower or faster depending on how far away the target is (so if i'm aiming at a far away wall the rocket will go impossibly fast, but if I aim at the ground in front of me it goes as slow as molasses).

What am I doing wrong? How could I get my rocket to travel to my target GameObject at a set speed that I have control over?

more ▼

asked Mar 19 '10 at 10:52 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

How does the code that actually steers the rocket towards the moving target look? This code is executed only once when it's fired, so it affects the movement only on the first frame.

Mar 20 '10 at 07:08 AM StephanK

Sorry, I should had clarified. It is not a "moving target" in the sense that I want the rocket to home on the target, I just meant the target is not stationary (it moves when the player changes where he is looking).

Mar 21 '10 at 09:28 PM PrimeDerektive

The code looks basically OK, and if you don't normalise the direction you will indeed get a different speed depending on how close the target is. Try declaring initialSpeed explicitly as float and set its value in the inspector.

Mar 24 '10 at 03:17 PM andeeee ♦
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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:

x1793
x1676
x318
x253
x90

asked: Mar 19 '10 at 10:52 PM

Seen: 2534 times

Last Updated: Mar 20 '10 at 01:32 AM