- Home /
bullet rotation as its affected by gravity
Hi, i am trying to get a bullet to rotate as it moves and is affected by gravity. So if the bullet speed in 0m/s it will just rotate 90degrees and fall straight down. My plan was to "make" a triangle of the "start" and "end" position of the bullet, ofc the start and end position changes alot during the bullets flight.
So my code looks like this
tempDistance = Vector3.Distance(startV,endV);
angle = Mathf.Rad2Deg * (Mathf.Asin(hightdecrease/tempDistance));
Vector3 test = new Vector3(angle,0,0);
transform.Rotate(test);
highdecrease is B in the picture above and i think tempDistance will be C.
But this doesn´t seems to work at all, i get very strange angles. Is the math correct?
What the hell dude ? You're using a game engine with a full physics system! Heh!
it will totally DO all that for you. Just throw the bullet and start it spinning, Unity will do everything.
Answer by Common_Horse · Jun 08, 2012 at 10:19 AM
You could try this:
//This would be the maximum speed of you bullet
float maxSpeed = 900;
//This is where the bullet was last frame
Vector3 lastPos;
void Update () {
//Find the velocity in m/s
float velocity = (lastPos - transform.position)/Time.deltaTime;
//Set up a ratio that will be used to rotate the bullet. If the velocity is maxSpeed, then the rotation will be 0 (forward, else it will rotate towards facing down (Assuming that +Z is your forward axis)
transform.eulerAngles = new Vector3(velocity/maxSpeed * 90, transform.eulerAngles.y, 0);
//Set lastPos so we can get the velocity next frame!
lastPos = transform.Position;
}
Very rough, but if you implement code like this, it should do what you asked!
-AJ
Answer by Eqric · Jun 08, 2012 at 10:30 AM
Thanks mate, i give this a try!
No problem man :)
If you need me to fix up the code, just give a yell!
-AJ
Your answer
![](https://koobas.hobune.stream/wayback/20220613072522im_/https://answers.unity.com/themes/thub/images/avi.jpg)