playMaker

Author Topic: Shooting: how to handle bullets? - A Discussion  (Read 7069 times)

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Shooting: how to handle bullets? - A Discussion
« on: July 27, 2013, 05:04:54 PM »
Hi guys - I wanted to make this thread, because i know there are many different ways of handling shooting and bullets, hit detection etc.

What method do you use for handling these things? What's the most efficient way, and what method is best for performance?

I am coding a shooter as a hobby project at the moment (not using Playmaker for this, but still), And i found that spawning a bullet and making it fly at a realistic speed (in my scale, using a force of 6000) makes the game FPS dip low whenever i spawn 3 or more bullets at the same time. On top of that, I use raycasts for detection at the moment, which also dips the FPS even more whenever i shoot, and because the bullets fly so fast, the raycast is inaccurate to a degree (sometimes bullet just flies through the wall because it was too fast  for raycast to detect it)

So are there better methods of doing this? Should you just spawn the muzzle flare at the gun, and then NOT use a bullet at all, and just raycast from the gun instead and use that for hit detection? I feel that having bullets that travel this fast is having a huge impact on performance.

Time to share your tips and tricks about shooting :)
Remember, you don't fail unless you give up trying to succeed!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15411
  • Official Playmaker Support
    • View Profile
Re: Shooting: how to handle bullets? - A Discussion
« Reply #1 on: July 28, 2013, 08:10:40 AM »
Hi,

 Have you tried particles?

bullets is really a matter of context. if you need a missile that has a trajectory, then simply fire a real physics gameObject with a collider etc etc, no need for raycasting then. also, if your missile has a complex shape, make sure you use a simple collider like a capsule or a box, else it will increase computation costs. Also play with the physics layer system to only have your missile collider work in a specific layer instead of trying to handl everything in the scene. that will help as well.

if your bullet is a straight line and you know the outcome even before firing, then don't use physics at all, simply animate an object. this will be simpler. and raycast only once, when the user press fire.

don't have a bullet raycasting as it flies, I can't think of an exmaple where it's needed, simply use a collider for this. That's exactly what colliders are meant to solve.

bye,

 Jean

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Re: Shooting: how to handle bullets? - A Discussion
« Reply #2 on: July 28, 2013, 04:55:55 PM »
Hi jean - Yes i was gonna try just using a one-time raycast as the player shoots and just spawn a muzzle flash particle and actually not have any bullets at all.

Would you know how i could, in a top-down shooter, add sway to the gun as you fire using raycasting?

Example:
- Player sees character from the top
- Character always looks at the mouse (player moves mouse around, character rotation follows)
- Player sees an enemy and moves the mouse over the enemy, making the character face directly towards the enemy
- Player clicks the mouse, making the character "shoot" towards the enemy
- The weapon is some sort of assault rifle, so the longer you hold down fire, the more the "shots" should spread (decreasing accuracy to discourage spraying)
- Shooting does not use projectiles, but rather fires 1 raycast for each shot fired
- How do i show / indicate to the player where his shots are going (how accurate they are) If there is no visible bullet? and how do i control the raycast direction? Is it as simple as to just get a random float between X and Y and use that for the raycast direction on the Z axis?

« Last Edit: July 28, 2013, 04:57:53 PM by KozTheBoss »
Remember, you don't fail unless you give up trying to succeed!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15411
  • Official Playmaker Support
    • View Profile
Re: Shooting: how to handle bullets? - A Discussion
« Reply #3 on: July 28, 2013, 05:03:37 PM »
Hi,

 Yes to all. As for indicating the shoot, you can use line renderer or vectrosity, or a simple box that you position and stretch in one direction to represent the shot. Having some sort of particle or visual impact effect on the hit point is also going to be mandatory I guess, that's also helping.

bye,

 Jean

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Re: Shooting: how to handle bullets? - A Discussion
« Reply #4 on: July 29, 2013, 10:35:39 AM »
Thanks, that is the direction I will go with this then :)

much appriciated!

_______________

I'd still like to invite others to join the discussion - I'm sure there are others out there who are in doubt about how to handle shooting, so if you have any tips, tricks or ideas on how to handle bullets in a specific situation then feel free to add a reply :)
Remember, you don't fail unless you give up trying to succeed!

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Re: Shooting: how to handle bullets? - A Discussion
« Reply #5 on: July 30, 2013, 12:43:21 PM »
Hi Jean! As far as i can see, it is only possible to raycast in 4 directions, left, right, forward and backward. Would I have to attach the raycast behaviour to a gameobject that would then rotate to simulate the guns inaccruacy? so rotating the GO instead of the raycast direction, and just raycast fowards instead?

Would that work?
Remember, you don't fail unless you give up trying to succeed!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15411
  • Official Playmaker Support
    • View Profile
Re: Shooting: how to handle bullets? - A Discussion
« Reply #6 on: July 30, 2013, 04:16:36 PM »
Hi,

 You can raycast in ANY direction, simply pass a direction vector. for example, use the transformdirection action to get the direction of a GameObject, and it will fire in that direction, if you want to change the angle after having retrieve a gameObject direction, then it's a bit more tricky, but achievable if you use quaternions operator. So get the quaternion of your gameObject, and then rotate that quaternion by a given angle to get a vector3 that you can then finally inject into your raycast :)

 you get can quaternion actions here:

https://hutonggames.fogbugz.com/default.asp?W967

bye,

Jean

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • View Profile
    • Pixel Life - portfolio
Re: Shooting: how to handle bullets? - A Discussion
« Reply #7 on: July 31, 2013, 08:58:41 AM »
yeah, this makes sense :) thanks! I have used playmaker for such a long time and only really used the basic actions so when i try solving problems, the more advanced actions completely slip my mind because i've never needed to use them before :)

Thanks!
Remember, you don't fail unless you give up trying to succeed!