playMaker

Author Topic: Gun recoil / kickback  (Read 7452 times)

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Gun recoil / kickback
« on: November 28, 2012, 10:29:53 AM »
Hi guys! I've been messing around with gun recoil and have been trying to add rotation to my camera when the player fires a bullet, but can't seem to make it work - do you guys have any idea why it won't rotate? I read on the unity forum a guy suggesting to make a sandwich for the camera, but im not really sure what he's talking about

Sorry if this is a noob question, but im still quite new to playmaker, and the search bar came up with nothing =/

Thanks in advance - koztheboss
« Last Edit: November 28, 2012, 10:46:40 AM by KozTheBoss »
Remember, you don't fail unless you give up trying to succeed!

DARK_ETERNAL

  • Full Member
  • ***
  • Posts: 110
Re: Gun recoil / kickback
« Reply #1 on: November 28, 2012, 10:46:37 AM »
So I guess it's more like an FPS what you're doing...
Are you rotating your camera? Perhaps an iTween rotation might help you since it's a default rotation?
Check out our new game: Nitro Chimp. Now live in App Store!

Trailer:
Download: https://itunes.apple.com/app/nitro-chimp/id557150450?l=en&mt=8

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: Gun recoil / kickback
« Reply #2 on: November 28, 2012, 10:48:19 AM »
Yes, it's an FPS project for school - What im trying right now is an iTween rotate on the camera indeed - but it won't do anything =( I've looked through the states and it goes to the state successfully when i shoot but it won't rotate the camera

Could it have something to do with the fact that camera is locked to the mouse cursor?
Remember, you don't fail unless you give up trying to succeed!

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Gun recoil / kickback
« Reply #3 on: November 28, 2012, 11:07:15 AM »
probably. Whenever you use scripts to controle the camera movements (like CameraLook etc) you should always make the camera a child of an empty (at 0,0,0).
Then a sanwich would probably be an overlapping animation, like you can have the camera look around in local space and then use the empty to rotate it extra.
Since that was probably incomprehensive, let's say it like this:
If you rotate a gameObject, every child of that gameObjet is rotated as well. But their rotation values (the local/ the ones in the inspector) always stay the same. So most well made scripts apply their transforms to local space so it doesn't care if a parent is meanwhile rotated as well.
So if you rotate the parent empty of the gun, it should not be affected by any local script on the gun (unless the script works in global space).

Worth trying either way :)
Best,
Sven

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: Gun recoil / kickback
« Reply #4 on: November 28, 2012, 01:34:21 PM »
got it, will try this out! thanks :)
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
    • Pixel Life - portfolio
Re: Gun recoil / kickback
« Reply #5 on: November 28, 2012, 02:09:23 PM »
Hey! :) I added an empty GO as parent to the camera and added the recoil in x axis in local space for it

It all looked to work exactly as i wanted until i let it recoil to the point where the camera edge was rotated with the empty so far back that it couldn't look down to see straight ahead (the rotation of the camera "followed" the empty - so if empty rotated 10 up on X axis - the new point i was looking at was now "straight ahead" for the camera - so when too much recoil has been done, the axis is so far back looking straight into the air is known as "normal straight ahead look" for the camera, which means there is a limit as to how for down i can aim with the camera so i can't see straight anymore =( any ideas on how to solve this? my camera and character is from the standard asset 1st person controller BTW :(
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
    • Pixel Life - portfolio
Re: Gun recoil / kickback
« Reply #6 on: November 28, 2012, 02:13:02 PM »
would making the camera rotate in global space instead of local solve the issue? my logic is that the camera rotation then wouldn't be affected by the empty GO (camera would still see the recoil and jump up but it would then actually rotate up on its x axis to look up, rather than rotate the actual axis itself to look up, yeah?

As always, your help is soooo appriciated <3
Remember, you don't fail unless you give up trying to succeed!

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Gun recoil / kickback
« Reply #7 on: November 28, 2012, 02:35:37 PM »
I suppose you're using the default unity mouse look script? In that case there should be a property like min and max x . In the same state you use to rotate the empty, drag in the script component from the inspector right into the playMaker action tab, and then release the mouse button there. That should give you the option to get or set a property on that script. Choose get property. Then drag it in again and choose set property (do this for both min and max).

Now, this is sounding a bit tricky, but it's simple, really.
Create 2 new float variables called MIN and MAX . Duplicate your set and your get property actions. Then set the one up, so you get the minimum X and save that to MIN. Set another up so you get the maximum X and save that to MAX.
Then do the same for the 2 set property actions. Make the set property actions every frame (just the set, not the get).

What we've got now is, it gets the min and max X values from the script and applies them every frame to the same script. So nothing should change really, other than that state probably running forever.
Now next is the fun part. Now that we have the min and max values we can actually change them however we like. The most convenient way would be the ease float action, which works like iTween move.
So let's say your gun recoil is 30 degrees and happens over 0.4 seconds linearly.

What we do is use "ease float" and tween the min and max values to a new pair of variables, let's call them NEWMIN and NEWMAX. So before the ease float actions, add a pair of float operator actions. They'll look something like MIN + 30 = NEWMIN . (same for NEWMAX).

So what should happen now is the gun should start to recoil, and as it rotates, the Minimum X and Maximum X on the script should update every frame to a new value which just as fast as the camera recoils turns from it's initial MIN and MAX values to the NEWMIN and NEWMAX values.
So without any noticable restriction if you look way up, the min and max values will always allow you to look around just like you did before.

I hope this made sense. My ideas are never simple I know :D
Best,
Sven

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: Gun recoil / kickback
« Reply #8 on: November 28, 2012, 03:06:59 PM »
oh my god, reading through this i was actually able to make sense of it! Will try that, thanks! will have to report back tomorrow afternoon though - work /sigh

Thank you so much for coming up with this solution! <3
Remember, you don't fail unless you give up trying to succeed!