- Home /
Bullet ricochet that aims towards a target.
Hello,
I am making a game in VR where bullets ricocheting is one of the key features, to make this "feel" good what I would like is at the point of ricochet to look for any enemies in a "cone" shape in the direction the ricochet would be going and alter the trajectory of the shot so that it more reliably hits the target. Aim assist but after the bounce essentially.
Initially I have been using Vector3.Reflect with a variable aim bias to go towards the target however this feels very unnatural when aiming away from a target (and currently only works on a single target). What methods/functions can I look to use to achieve the result I am looking for?
private void Bounce(Vector3 collisionNormal)
{
var speed = lastFrameVelocity.magnitude;
Vector3 bounceDirection = Vector3.Reflect(lastFrameVelocity.normalized, collisionNormal);
var directionToPlayer = playerTransform.position - transform.position;
var direction = Vector3.Lerp(bounceDirection, directionToPlayer, bias);
//Debug.Log("Out Direction: " + direction);
rigidbody.velocity = direction * minVelocity;
}
Thanks for your time and help. Matt
Answer by JPhilipp · Feb 16, 2020 at 02:55 PM
Interesting concept! You could, post-bounce collision, do a SphereCast forward towards the new bullet direction. SphereCasts are like rays with ray width, so you can set a radius big enough to be more forgiving. If the SphereCast hits an enemy, you can then instantly calculate the new direction that would be needed for hitting it normally, and quietly set that as new rigidbody velocity. This guarantees you wouldn't always alter the path, but only when beneficial.
In addition, you can try out always doing a normal RaycastHit hit to see if the SphereCast "path cheat" is needed in the first place, and skip if not.
Good luck!
Thanks I managed to get it working with SphereCasting and a little vector3.lerp and initial tests feel pretty good. Thanks for the advice
Your answer
Follow this Question
Related Questions
How to force physics 1 Answer
2D 360 degress platformer example needed 0 Answers
Punching a crate 0 Answers
Two Bone IK constraint with more than 2 bones on my arm. 0 Answers
Collider IsTrigger won't work correctly with close second Collider 0 Answers