I’m trying to implement recoil on the gun barrel of a tank turret. The barrel will recoil, but it initially moves only a small amount, followed by a pause, followed by the remainder of the movement. I’ve tried various methods that I came across in the forums, but they all give the same result, so there is definitely something wrong with my understanding of how translating along a local axis works. Can anyone shed any light on why my code doesn’t work? Thanks.
void Update()
{
if (isRecoiling)
{
barrel.Translate(Vector3.right * Time.deltaTime* 1.0f);//This works, but jumps in two steps.
Debug.Log(barrel.localPosition.x);
if (barrel.localPosition.x >= barrelRecoilAmount)
{
isRecoiling = false;
}
}
}
Is there anything else that’s moving the barrel anywhere in your game?
The script appears as though it should move the barrel by a small amount each frame.
PS. you needn’t multiply deltaTime by 1f… anything multiplied by 1 is just ‘anything’
No, there’s nothing else in the script, and this is the only script that I have in the project.
When I look at the console, I can see the values jump, I just don’t understand why. The values are (approximately):
0.02, 0.04, 0.37, 0.39, 0.406
The 1.0f is just a placeholder for speed. I’m trying to get it to work, before I play with speed.
Ahh! Placeholder for speed makes perfect sense, cool.
Okay, good to know it’s the only script (rules out other possibilities).
Those numbers are kinda “crazy”. 330ms between update 2 and 3, approx.
Unless I’m really missing something, that’s saying that your frame rate really plummets there for a brief moment, and that’s why you see the “pause” sort of then huge spike… Dunno if you ever watch the ‘stats’ window or have an FPS viewer (script) or anything… but with just 1 script (that one!) in the project, I’m very confused.
No, I haven’t used any other monitoring other than the Log statements. Unity and VS2017 are the only things running on my computer, and the results are always the same, no matter how many times I press play. Yup, I’m confused to, because this is the solution that pops up most in answer to similar forum questions in the forums.
Ya, it’s unnatural lol. (err, maybe I should say “unusual” ). Any game could have a spike or something, but yours is quite long and , as you said, consistently long.
if you put this on a cube :
void Update()
{
transform.position += Vector3.right * Time.deltaTime;
}
Just 1 cube with this script, and the rest of the scene “disabled” … do you still get that stutter?
If you have what appears to be a long frame, it’s definitely time to open up the Profiler window and figure out what’s taking so long in that frame.
I get exactly the same behaviour, except that there is no code to stop the movement so the cube, after the initial lurch, moves smoothly.
huh… that is truely strange. You could/should profile, maybe, as @StarManta suggested. Sorry I couldn’t be more help
I tried playing the scene with the Profiler open but everything looks OK. The “WaitForTargetFPS” is 87.5%. The only other thing that takes up any amount of time is the Camera Renderer, which is at 6.7%.
That’s OK, methos, thanks for trying. I hate these kinds of problems, as it usually means that something really obscure is happening and for a noob, that’s brutal.
I would expect to see a ‘spike’ in the profiler window - if you click on that spike it’ll show you the data from that one slow frame. Could you post a screenshot of your profiler window?
It seems like the Profiler doesn’t actually start showing results until the initial blip has passed. With nothing in the scene, the Profiler seems to have a lag. Not sure if this helps.
There is lag with nothing? Ouch
What about if you profile your recoil though, you said it happens everytime you fire? Would you see it then?
Yes, I get it every time, and it’s not like my machine is slow. Here is what the Profiler looks like with just the cube.
Not sure why it won’t allow the jpg.
That sucks, man. Maybe someone will glean some insight to your issue.
I have to get outta here… get some sleep Good luck!
Try uploading to imgur or something and linking to it?
I reloaded the Space Shooter tutorial, and I seem to be getting the same start-up stutter there as well. Is this a known issue with Unity?
I tried linking to a Dropbox link.