- Home /
Use transform.LookAt with Raycast with a delayed bullet?
So I'm just wanting to create a weapon for my game, and there's 2 ways to do that: Raycasting and with actual physics. But because bullets are too fast for physics, raycast is the other option. I don't want to use straight raycast because that's like a "lazer", and I Want a bullet, so something that's like delayed by a few milliseconds before it "hits" the target. Anyone got any ideas?
Not really an answer, since in truth this sort of thing would be up to personal preference. However, I do not think your idea would feel right. A bullet travels so fast that the actual reaction time from a person would be more than enough to implement what you are looking for. Putting in code to delay the actual bullet by a few milliseconds seems unnecessary. Have you tried to implement the Raycast system yet? I'd try that and see how if feels before putting in the delay.
Answer by MikeNewall · May 30, 2014 at 07:07 PM
Using a raycast alone means your projectiles will always follow a linear path, and hits will be instantaneous. If you want to simulate a bullet with a ray cast there are a few things to take into account.
Muzzle velocity is the speed that a projectile leaves the weapon. High caliber rifles have a max muzzle velocity of around 1700 meters per second which means that if we say one unit in Unity is a meter then the distance traveled in a second would be 1700 units. The further you are from the target and slower the bullet the longer it will take to hit the target. If your weapons are used at short to mid range then delaying the ray cast would be barely noticeable, however if you require longer ranged weapons then delaying the raycast is more important. The speed of the bullet will also decrease over its life time with air resistance.
You also need to take into account the accuracy. Weapons don't hit the same point each time they fire like a ray cast does. You can simulate accuracy by adding noise to the position that you cast the ray from.
Because a ray cast by its nature is linear your projectile aren't affected by gravity so they dont have a true trajectory. A projectile moved by physics however would, but it'd be moving too fast to detect a collision. Instead of casting a ray from the weapon you could cast a ray from the tip of the projectile to the position that the bullet will be next frame which can be calculated from the bullets heading and speed. If it hits anything then you know that the next frame the bullet will hit the object.
Not even sure if that answered your question :)
Your answer
Follow this Question
Related Questions
How do I create bullet hole for my gun? 0 Answers
Gun Script 1 Answer
Raycast shooting in the middle of the screen 1 Answer
Best way to shoot physical bullets? 2 Answers
gun problems 1 Answer