- Home /
Simulating realistic bullet physics.
I am developing a FPS game and I want to have bullets drop over distance and actually take time to travel but I don't want to use rigidbodies because they're too unreliable for detecting collisions.
In my head I have thought maybe I could take the angle that the weapon fires at and use that with an algorithm of some sort that will give me points along the trajectory that I can fire rays at different angles along with collision detection. I'm unsure however how I could accomplish this.
Could I have the rays fire at different points and time until there is a collision detected so the bullet doesn't travel instantly but also not miss any collisions?
Answer by Owen-Reynolds · Nov 16, 2012 at 04:51 AM
That's basic calculus (which means it's not really calculus.) Estimate the curve by doing lots of little straight lines. The official math term for this is the "shooting method."
The "bullet" would be just a set of numbers -- position, and x,y,z speed. Maybe spawn a bullet empty with that script. Each frame move the bullet forward and add gravity, then raycast from start to end. Then decide you want to do that in three steps/per frame (move 1/3rd, add 1/3 gravity, three times.)
If you do it adding gravity before the move (always a little too low,) then again adding after (always a little too high,) you bracket the real value, so can play with steps/frame until the error is tiny.
May as well add the wind, as long as you're doing all that work.
Oh god not calculus.
Could I fire a ray like 50 metres and then spawn an empty at the end and rotate it downwards and fire another ray and do this until there's a collision?
What about using a rigidbody and capturing it's current position every so frames and fire a ray to detect collisions?
There's no need to spawn a trail of ray end-points. You can just have a `bulletPos` variable, with no backing gameObject. and raycast from `bulletPos` in direction`bulletSpd` for distance `bulletSpd.magnitude`. Then set `bulletSpd.y+=grav;` and `bulletPos+=bulletSpd;` and repeat.
You might want to test-spawn tiny no-collider spheres at each point, just to check that the curve looks O$$anonymous$$.
Answer by Major · Jun 20, 2017 at 05:23 AM
Post is old, but somebody might find this useful! Just apply the script to a spawned GameObject, and everything else will be taken care of.
using UnityEngine;
using System.Collections;
public class BulletPhysics : MonoBehaviour {
public float gravity = -9.81f;
public float velocity = 350;
public float maxTime = 5f;
private Vector3 origin;
private Vector3 cameraDir;
private float increment = 0;
void Start(){
origin = transform.position;
cameraDir = Camera.main.transform.forward;
}
void FixedUpdate () {
//displacement = v0*t + 0.5at^2 + origin
//Debug.DrawLine (disp(increment), disp(increment + 0.02f), Color.red, 10);
RaycastHit hit;
if (Physics.Linecast(disp(increment), disp(increment + 0.02f), out hit)) {
Destroy (gameObject);
}
if (increment > maxTime)
Destroy (gameObject);
transform.position = disp (increment + 0.02f);
increment += 0.02f; //<--- fixedupdate time (value in Time settings)
}
Vector3 disp (float t){
Vector3 vel = velocity * cameraDir;
Vector3 disp = origin + vel * t - (playerDown * Mathf.Pow (t, 2) * gravity * 0.5f - Vector3.up * t);
return disp;
}
}
Looks like I didnt fully fix this version, whoops.
playerDown is a unit vector thay denotes the relative "down" direction for the player. For any normal shooter senario this is equivilant to Vector3.down.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer
Rotating an Object to normal 0 Answers