I’m using rigid-body force and aiming at the player with the ‘transform.LookAt()’ function
It doesn’t seem to be working too well. Some times the collision isn’t registered, especially with movement, and the speed being over 5000 doesn’t register at all (and it doesn’t look realistic at this speed).
Not sure if this is the best method for something like this.
I’ve attached the project (it’s only 4.6mb) if anyone’s able to take a look and offer suggestions i’d appreciate it.
//Verylazysingletonfornow
public static Player Instance;
[HideInInspector]
public int health = 100;
void Awake()
{
Instance = this;
DontDestroyOnLoad (gameObject);
}
I downloaded it and tested it, the problem is definitely the speed.
Your bullet objects are moving too fast for their size for collisions to be measured reliably.
I would either try:
Increasing the solving iterations (Edit->Project Settings->Physics)
Note that increasing the number of solving iterations demands more performance from your game, but makes the physics more accurate, especially at higher speeds. I set the iterations on this project from 6 to 50, and it didn’t make much difference. If you’re hellbent on using physics for you ballistics, I would make it so that the collider is stretched along the vector of the bullets movement, that way the collision would be less likely to ‘miss’ when the bullet jumps such big distances between frames.
Instead of using bullet simulations, use ray casting or calculate the path the bullet WOULD take if it was using physics, and see if the player intersects it.
My physics based bullets cast a ray in front of them to test for any collision that may happen in the next frame(or so). Ray direction is based on rigidbody velocity so it follows the bullet path.
//meanwhile.... somewhere in bullet Update()....
if (Physics.Raycast(transform.position, myRigid.velocity, out hit, (myRigid.velocity.magnitude/Time.deltaTime), ~LayerMask.NameToLayer("Projectiles")))
{
// bullet hit something in
}
RayCasting is definitely the answer. For something as high speed as this do you even need the bullet GameObject? Can the user actually see the objects in your scene? If not just run with a ray cast and can the bullet altogether.
Cheers for taking the time to download it I appreciate it.
I didn’t even know you could increase the physics solving! I think i’ll take on yours and others raycast ideas, it seems like that’s a much better solution. though it’s a really cool idea of increasing the collider.
That’s a really good call, I thought it would be nice to see the bullet, but I guess you could get away with it with just the muzzleflare and animations etc… Cheers for the advice!
but hold on, increase the time step for fixed update, and setting the rigidbody to calculate everything collision continously requires more cpu, the more you increase the more cpu will be needed, consider that and the amount of bullets. And you can also use a line renderer and some particle effects to fake the effect of a bullet flying.
or just remove the damage portion of the script from his current bullet and just fire off that rigid body as a tracer only… kinda like how call of duty does it (watch the kill cams… you die well before any visible rounds are near you). Do damage with raycasts and just fire the tracer as visual reference only.