x


How can I calculate velocity without using Rigidbody?

Hello everyone!

This is a problem I have run into while making my space shooter style game. I have a script that I put onto my "targets" in which I get its rigidbody.velocity. I then use that script on my aiming turrets to create a believable "lead time" effect and actually hit when the turret fires. However, I am running into a problem with my "missiles." In order to get them to fly to the target, more or less, I am using transform.translate. Using that and a speed variable (and time.deltatime), I am able to get my missile to where it needs to go.

Now, I have a rigidbody on the missile for collisions and such, but my script doesn't appear to be getting any information about the speed / direction of the missile. I presume its because I am moving it via a transform translate and not a rigidbody.AddForce, but it really behaves a lot better this way.

Is there a way I can "fake" the speed information I would normally get from rigidbody.velocity on my missile object that is moved by transform.translate?

more ▼

asked Dec 16 '10 at 02:26 PM

Bob5602 gravatar image

Bob5602
603 15 17 29

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

2 answers: sort voted first

Simple & crude velocity measure:

You can sample the world position on this frame and store the result from the previous frame. You now have two points of reference. Take into consideration how long the time slice was between the two points to derive the velocity. I think the code would be:

var velocity = (current - previous) / Time.deltaTime;

If the leap (current - previous) was big, say 5 units to the right and the time slice was small (1/60), we would be going very fast. 5/(1/60) = 300 units per second.

more ▼

answered Dec 16 '10 at 02:36 PM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

Hmm yeah, thats what I was thinking. Getting back into the roots of it all :)

That should be doable, with an extra variable to store the last position. So if I put this into an update function, it would look something like

var previous : Vector3; var velocity : Vector3; function Update(){ velocity = (transform.position - previous) / Time.deltaTime; previous = transform.position; }

?

Dec 16 '10 at 02:42 PM Bob5602

Bingo. That's right.

Dec 16 '10 at 02:44 PM Statement ♦♦

Cool thanks. Thats what I had originally thought but wasn't sure if the differences per frame would be significant enough.

Dec 16 '10 at 02:46 PM Bob5602

Erm, it should. Otherwise you can instead multiply with (1.0f / Time.deltaTime). Remember to check Time.deltaTime for 0 since division by zero causes exceptions. This happen if you set your time scale to zero (i.e. pause the game).

Dec 16 '10 at 03:18 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

Hi I was working on the same thing as this recently, and I managed to get something that works for me, even if the velocity hits zero. I only set this up to work on the Y axis but should be easy easy enough to calculate the other axis too.

var camPos : Transform;

Update () {

StartCoroutine(curVelocity(0.1));

}

function curVelocity(waitTime : float)

{

     var previous : float;
     var current : float;
     var velocity : float;
     previous = camPos.position.y;
     yield WaitForSeconds (waitTime);
     current = camPos.position.y;
     velocity = current - previous;
     if (velocity == 0)
     {
    velocity = 0.000001;
     }
     velocity = velocity / waitTime;
     if (velocity < 0.0001 && velocity > -0.0001)
     {
    velocity = 0;
     }


     print (velocity);

}

more ▼

answered Jan 10 '12 at 08:50 AM

nickazg gravatar image

nickazg
31 3 3 3

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

x3570
x1862
x330

asked: Dec 16 '10 at 02:26 PM

Seen: 5030 times

Last Updated: Jan 10 '12 at 08:50 AM