- Home /
[Closed]transform.position not updating fast enough
I have my gun following my player using transform.position, but when I move the gun glitches back and forward because it is simply not fast enough to keep up with my player and look somewhat decent. I have been getting around this before by setting the gun to be a parent of the camera object, however now that I have a gun pick up script this is no longer a viable option. Here is the code that updates the guns position to the players transform:
Vector3 centerOfCamera = playerCamera.GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f,0.5f, playerCamera.GetComponent<Camera>().nearClipPlane));
transform.position = Vector3.Lerp(transform.position, centerOfCamera + Quaternion.Euler(targetXRotation, targetYRotation, 0) * new Vector3(0, 0, currenRecoilZPosition), hipToAimSpeed);
All of this code is placed within the void lateUpdate() function.
Thanks for reading my question and I hope that one of you can lend a helping hand.
Have you tried to place this code in the regular Update() and see if that helps?
I'm not sure why having a pick up script would make your previous solution no longer viable.
@Doc$$anonymous$$cShot, It helps a little, but it still very buggy and jumpy. @Habitablaba, I just have always assumed that there is a better way of doing things, I mean if there is no fix then I am happy putting the gun as a child of the camera.
This sounds more like a script order problem rather than a speed problem. Consider putting the code in LateUpdate() or specifying the execution order of scripts:
Edit > Project Settings > Script Execution Order
I think having a parent/child relationship between the gun(or player, even) and the camera is a fine idea. It is certainly simpler than trying to force the two to stay together some other way, and at the end of the day, it works :)