I am trying to figure out how to apply a rotation to a bullet (the model is huge) according to the curve that he does during the trajectory from the shooting to the stop on the land. The projectile does a curve because of gravity, but the object does not rotate the front of the projectile where it is going.
Here a picture representative:
Please forgive me this dreadful paint. hehehe
You could adjust rotation according to delta velocity. Just compare the bullets velocity each frame, to last. Lets say, in your second diagram, that the first arrow, has a velocity of 1,0, and the last, 0,-1. In both cases you can retrieve a rise and run value. First case, rise is 0, run is 1. Second case rise is -1, and the run is 0. Knowing the rise and run, you can gather the angle of the tangent. You can use this to set the angle of z.
hmm… this is ok for a 2D space. Tan = (Opp/Adj)
But I don’t know how i apply it on a 3D space. Do you have the formula? I can transcribe it to a function C# function, than I can rotate it toward the pulling force (Gravity).
Sorry i didn’t mentioned it before. It is a 3D game.
Best regards,
PJRM
you know guys, when renman3000 talk about tangent, i thought “What!? must to be a simple way to do that…”
And then mouurusai came and told me about rigidbody.velocity… I realise what i need to do. It’s so simple!!! Just 1 line make it work.
Awesome.
I did have a formula which does work, but it requires me getting the positions, of a few things and is generally messy compared to what you guys are going with. However, I am using 2D, and the object in question is rotating in such a manner that it disappears from the orthographic camera.
You could simplify this a little if you wanted to by using the transform.forward property. I’m not sure if it’s good practice, but you can actually set this vector whenever you want, so
This would achieve the effect of gradually rotating an object to face its velocity vector, regardless of what it is, and would also serve to eliminate some of the “jittering” when its velocity was close to zero. You could also use Vector3.sqrMagnitude to check if it is moving faster than a certain threshold too.
@renman3000, this should work fairly well in 2D as well! If your velocity vector is zero along any axis, it should just point the object such that it’s forward vector has a zero on that axis as well. This way, if you’re locking the object’s velocity to zero, it will never rotate to where you can’t see it!