terewbowl.blogg.se

Realistic fps prefab starting weapon
Realistic fps prefab starting weapon







To be able to do that we have to use a method called numerical integration. To make it more good looking I've decided to first calculate the time it takes to hit the target so the trajectory curve stops when the target has been reached. TutorialBallistics.CurrentIntegrationMethod(h, currentPosition, currentVelocity, out newPosition, out newVelocity) Calculate the new position of the bullet LineRenderer.SetPosition(index, currentPosition) Vector3 currentPosition = įor (int index = 0 index < maxIndex index++) Vector3 currentVelocity = * bulletSpeed Int maxIndex = Mathf.RoundToInt(timeToHitTarget / h) How long did it take to hit the target?įloat timeToHitTarget = CalculateTimeToHitTarget() Display the trajectory path with a line renderer So create a method called DrawTrajectoryPath() and add the following. If we can make such a trajectory it will also be easier to see what's happening, because bullets are sometimes very fast. Theta2 = Mathf.Atan2(top2, bottom) * Mathf.Rad2Deg īut wouldn't it be cool to also see the trajectory the bullet takes when it is flying towards the target.

realistic fps prefab starting weapon

Theta1 = Mathf.Atan2(top1, bottom) * Mathf.Rad2Deg Reset y so we can get the horizontal distance xįloat underTheRoot = (vSqr * vSqr) - g * (g * x * x + 2 * y * vSqr) įloat rightSide = Mathf.Sqrt(underTheRoot) Vector3 targetVec = targetObj.position - gunObj.position Void CalculateAngleToHitTarget(out float? theta1, out float? theta2) Returns 0, 1, or 2 angles depending on if we are within range

realistic fps prefab starting weapon

Which angle do we need to hit the target? If you then press play you should be able to drag the Target cube around the scene (if you are in Scene view) and the barrel should rotate up and down. and a method called CalculateAngleToHitTarget() which will apply the equation from Wikipedia. GunObj.localEulerAngles = new Vector3(360f - angle, 0f, 0f) decreases from 360 degress when we are rotating up pointing "forward" position, the angle increase from 0, but our gun's angles

realistic fps prefab starting weapon

The equation we use assumes that if we are rotating the gun up from the So create a method called RotateGun.ĬalculateAngleToHitTarget(out highAngle, out lowAngle) The artillery will try to hit the target from above, while the sniper rifle will try to hit the target from the front. The angle in the script called highAngle will rotate the barrel to a rotation similar to an artilley gun, while the lowAngle will give us a rotation similar to a sniper rifle. That equation will give us 2 angles or none angles if the target is out of range. To help us we are going to use an equation called Angle theta required to hit coordinate (x,y) from Wikipedia's Trajectory of a projectile. Now we will rotate the barrel so it aims at the target box. But lower is not always better because of rounding errors Or a more precise to get a more accurate result Can use a less precise h to speed up calculations public static float bulletSpeed = 850f Public class TutorialBallistics : MonoBehaviour Don't worry if you don't understand step size, I can guarantee that you will when the tutorial is over. Also drag the Target cube and the Barrel connector Game Object to the script. What we need is a script called TutorialBallistics, so create it. NewBullet.GetComponent().velocity = TutorialBallistics.bulletSpeed * transform.forward īut bullets flying out from a turret will not kill any targets. Add velocity to the bullet with a rigidbody Parent it to get a less messy workspace GameObject newBullet = Instantiate(bulletObj, transform.position, transform.rotation) as GameObject Public class TutorialFireBullets : MonoBehaviour If you press play you should see bullets fly out of the turret (if you replace TutorialBallistics.bulletSpeed with something like 30f). So don't forget to drag the bullet prefab and the Bullets parent Game Object to the script. The script will give the bullet a speed in the direction the barrel is pointing after parenting the bullet to its parent GameObject so we get a clean workpace. Make sure to not add any drag and the mass is not important. You also need to add a RigidBody to the bullet so it can fly. It's relatively self-explanatory, but the basic idea is to fire a bullet every 2 seconds from the Barrel connector Game Object, so add the script to it. So create a script called TutorialFireBullets and add the following to it.

realistic fps prefab starting weapon

This tutorial is all about ballistics so I believe the first thing we have to do is to make the turret fire bullets. When you are finished with this section of the tutorial you will have something that looks like this:









Realistic fps prefab starting weapon