Gun Following Camera Problem

public var MoveAmount : float = 1;
				 
				             public var MoveSpeed : float = 2;
							 
									     public var GUN: GameObject;
										 
										         public var MoveOnX : float;
												 
												     public var MoveOnY: float;
													 
													         public var DefaultPos : Vector3;
															 
															 public var NewGunPos : Vector3;
															 

															             

function Start () {


     DefaultPos = transform.localPosition;



}



function Update () {


			     MoveOnX = -Input.GetAxis( "Mouse X") * Time.deltaTime * MoveAmount;
				 
				 MoveOnX = -Input.GetAxis( "Mouse Y") * Time.deltaTime * MoveAmount;
				 
				 NewGunPos = new Vector3  ( DefaultPos.x+ MoveOnX, DefaultPos.y+ MoveOnY, DefaultPos.x) ;
				 
				 GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, NewGunPos , MoveSpeed+ Time.deltaTime);
}

I am trying to get the gun to follow the camera like CoD for example. But when i attach this to my gun it changes the position of it so I can only see the barrel.

This script is not working ( it changes the position of the gun ).

I am looking for it to follow the camera, like

That was the tutorial i found

First, don’t capitalize variable names (especially ALL CAPS which is only used on constants generally). Following the popular conventions means that Classes/Types and functions/methods should be capitalized.

if you want the gun position to be offset from where it is, just change it before setting the transform.position. Simply add it to x, or y, or z when you setup the vector.

Please also preview your posts and make sure the formatting is clean before posting (or edit the post afterward if you miss something). Remember, we are all just volunteer contributors here, so make it easy on your fellow developers when you are asking for free help, Do homework, research, try things, and post after you have gathered enough information to narrow down the possible replies you may get.

Sorry for not posting clearly, :(.

It seems it is changing the gun’s position…

Let me make some pics/

With script:

Without Script:

So I am assuming something in this script is making the gun change position. And not the desired effect either…

There is an error in your console you have to assing the GUN variable maybe thats the problem

Yeah i didn’t know why it was saying that because i did assign the variable…