I am working on a tank game currently and I wanted to add a kind of recoil system (you shoot the gun moves back a bit and slowly returns) everything works except for moving it here is a little bit of the code I have tried:
gunRecoilPos.localPosition = gunOrigin.localPosition - transform.InverseTransformDirection(gunOrigin.forward) * 0.5f; but this just doesnt work ;( Whenever the turret turns, the forward direction rotates with it.
I made a detailed sketch aswell :D
#transform.forward in local space (am giving up on this)
1 messages · Page 1 of 1 (latest)
I dont know if this is what you are looking for, but you could add a script to the tank turret, or what recives the recoil that constantly moves to the original local position with a little delay, and when you shoot you just move it and it will return
That shouldn't give problems
What you said already works, however I cant get it to work nicely. When turret faces forwards it works all good but if the turret faces up I need it to move on the Z axis (backwards inside the turret) and on the Y axis (a bit downwards) so basically in the opposite direction of where the gun is facing. I have a script where the gunOrigin stays on the original position of the gun and the gunRecoilPos follows it -0.5f on Z axis behind. Lets say we are looking at the turret from the top and the turret is facing this way → the gunOrigin.forward stays the same aswell so it is this way → now if the turret turns 90 degrees right and is now facing this way ↓ the rotation is not applied to the rotation and is still this way → the gun then moves to the right of the turret instead of inside the turret
it is though to explain
ok, after fiddling around with local and world space i am happy to report i did it :D