How To Keep My Weapon With My Camera?
Hello,
I've just run into a problem. My gun does not stay with my camera when it begins to recoil, and after the methods being to run, it "Lerps" back to where it is supposed to. Here is what issue I am experiencing:
Here are the methods, and I have properly assigned my weapon transform object to my weapon as well.
IEnumerator MoveToPosition(Vector3 newPosition, float time)
{
float elapsedTime = 0;
Vector3 startingPos = weapon.transform.position;
while (elapsedTime < time)
{
weapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
}
void recoilBack()
{
Vector3 zoomOutOffset = new Vector3(-0.01f, 0, 0);
if (zoomOutOffset.x >= 0.05f)
{
zoomOutOffset.x = 0.05f;
}
var zoomOutWorldPosition = weapon.transform.TransformDirection(zoomOutOffset);
StartCoroutine(MoveToPosition(weapon.transform.position + zoomOutWorldPosition, recoilSpeed));
}
void recoilForward()
{
Vector3 zoomInOffset = new Vector3(0.01f, 0, 0);
if (zoomInOffset.x >= 0.05f)
{
zoomInOffset.x = 0.05f;
}
var zoomInWorldPosition = weapon.transform.TransformDirection(zoomInOffset);
StartCoroutine(MoveToPosition(weapon.transform.position + zoomInWorldPosition, recoilSpeed));
}
Answer by Tony_T · Oct 10, 2015 at 06:52 PM
Make sure the weapon is child of the camera. Also if you have animated the weapon via Unity it probably uses the global positions so it's most likely it will return to the position your firstly animated. I would recommend animating the weapon in a the software you made the weapon so it is attached to the character and play it no matter where you are in the world.
Nothing to do with animations, read the code please, everything is already setup properly in the GUI.
You are not explaining your problem that good. On the title it says "How To $$anonymous$$eep $$anonymous$$y Weapon With $$anonymous$$y Camera?" then you say it has nothing to do with animation and that the GUI is already set up. It's just confusing.
I think you are misunderstanding. There is no physical animation for the gun, all of the gun movement is done in scripts and is not animated in any other 3D software. It is just a single model with scripts applied to it to have a recoil animation added to it. The methods I gave in my example are causing what you see in the screenshot. The screenshot only occurs when I begin to move while shooting, as the gun doesn't transform its position with the camera. That is the only problem, everything in Unity's interface is already properly set up, from weapon camera to main camera, and to bullet spawn game object's and more. The hierarchy is not the issue, it is the methods supplied. I hope that clears any confusion for you! Thanks!
Answer by SwiftLee · Oct 11, 2015 at 04:42 AM
No luck yet, finally fixed some of my sounds in the meantime... Any suggestions as to how I can get the gun to continue to recoil and move along with my player/camera? The methods I supplied are called when the IEnumerator Coroutines are run. Thanks for your help guys, please let me know if you have a solution!