playMaker

Author Topic: How to pool projectiles without ruining the next shot  (Read 2601 times)

daniellogin

  • Full Member
  • ***
  • Posts: 215
How to pool projectiles without ruining the next shot
« on: March 07, 2018, 10:20:35 AM »
I've just found that I should be pooling my projectiles. I did set this up, but now I'm finding a problem where the new projectiles are being effected by ones that just just been despawned/returned to the pool. It seems to be saving either rotation or some other force so the shots fire off crooked.

So how do I properly reset all of the properties which are being passed on from the previous shots to ensure the projectiles fly correctly?

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: How to pool projectiles without ruining the next shot
« Reply #1 on: March 07, 2018, 10:39:11 AM »
Pooled object keep all of their properties, they are just disabled, so you should reset all the parameters manually when you despawn them if you want to use them as tehy were whn you respawn then.
Available for Playmaker work

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: How to pool projectiles without ruining the next shot
« Reply #2 on: March 07, 2018, 10:43:45 AM »
Pooled object keep all of their properties, they are just disabled, so you should reset all the parameters manually when you despawn them if you want to use them as tehy were whn you respawn then.
I know that. I mean how do I reset them. What exact actions do I use? So far I've tried:

Set Rotation and Set Velocity to 0,0,0 before the object despawns.

Set Property -> Rigid Body -> Velocity + Angular Velocity + Rotation to 0,0,0 before the object despawns

These don't work.

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: How to pool projectiles without ruining the next shot
« Reply #3 on: March 07, 2018, 10:54:29 AM »
What are you using for moving projectiles? Don't forget to reset your variables in FSM's, if you're adding value to the velocity float, for example, it will start with the last registered value. States in FSM's are reset, but variables are not.
Available for Playmaker work

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: How to pool projectiles without ruining the next shot
« Reply #4 on: March 07, 2018, 11:05:42 AM »
What are you using for moving projectiles? Don't forget to reset your variables in FSM's, if you're adding value to the velocity float, for example, it will start with the last registered value. States in FSM's are reset, but variables are not.
The projectile object is simply spawned. Inside the projectile is an FSM that starts with Add Force on Z.

The projectile has it's last action as Pooler Destroy Self. I tried adding the previously mentioned actions in the same state just before the destroy.

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: How to pool projectiles without ruining the next shot
« Reply #5 on: March 07, 2018, 11:11:34 AM »
I had this same problem, and finally got it working.

Try adding Force 0 to your projectile, and turning the 'Is Kinematic' property off and back on (or on/off) before despawning.

I don't have access to my project at the moment to see exactly what I did, but I can take a look at it tonight if you are still having issues.

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: How to pool projectiles without ruining the next shot
« Reply #6 on: March 07, 2018, 11:16:44 AM »
While testing again now I see that the projectile seems to be facing in the correct location, but it is firing exactly on the world Z axis instead of it's local Z like it should. So no matter which direction I point the gun it goes the same way every time while not facing forwards.

So... it's losing it's sense of local self somehow after coming back from the pool?

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: How to pool projectiles without ruining the next shot
« Reply #7 on: March 07, 2018, 11:17:52 AM »
I had this same problem, and finally got it working.

Try adding Force 0 to your projectile, and turning the 'Is Kinematic' property off and back on (or on/off) before despawning.

I don't have access to my project at the moment to see exactly what I did, but I can take a look at it tonight if you are still having issues.
These didn't work. I think though adding a force of zero is effectively doing nothing. It's not setting but adding.

Kinematic off and then on before despawning didn't have any effect on my problem.

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: How to pool projectiles without ruining the next shot
« Reply #8 on: March 07, 2018, 01:04:50 PM »
I don't get it, if you use force to move an object, resetting velocity should work.

I googled a bit, and it seems you're not alone on this one. Check this thread and try out what works for you.

https://forum.unity.com/threads/reset-rigidbody.39998/
« Last Edit: March 08, 2018, 12:43:16 AM by krmko »
Available for Playmaker work

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: How to pool projectiles without ruining the next shot
« Reply #9 on: March 07, 2018, 05:43:26 PM »
If I remember right, I think I may have done all of those things (setting velocity, kinematic on/off, resetting rotation, etc) BOTH immediately upon spawning AND just before de-spawning. Try that and see if overkill helps!
« Last Edit: March 07, 2018, 05:46:58 PM by ToxicFrog »