- Home /
FPS Gun Moves Weird When FPS Controller Turns up or Down
Thank you for your time :)
So basically I wanted to make a simply FPS, and I am currently using the object and texture from the FPS tutorial to give my game a Rocket Launcher. I'll put in my own graphics for guns, but I want to get the basic framework working :)
I went through most of the FPS tutorial, but had an issue with some assets that it said were in "Standard Assets" not being there. Regardless, I got the rocketlauncher attached to the FPS nicely. However, when I tried importing the rocketlauncher mesh and texture, it looked fine. I tried dragging it to child it to my FPS controller's camera. I saw a problem people were having this was due to them childing the gun to the controller instead of the camera, but this doesn't appear to be my problem. If someone wants I'd be happy to post a screenshot of what it's doing if it's unclear. Basically the gun moves away from the player when your First Person Controller looks up, and back when your controller looks down, leading to a lot of distortion. Thanks!
Answer by Dev Solo · Jun 24, 2012 at 09:46 PM
I'd say start from scratch if you're having troubles with it. It should only take 30 seconds to load in a new FPS controller and attach a gun to its camera.
The only thing I can imagine to explain that is if you might have added a "mouse look" script to the gun as well as the camera.
Tried making a new project and it worked! However, I did figure out what my problem is, but I don't know how to fix it... I have a script attached to my FPS controller, and somehow these things which affect FOV, etc. are also affecting the gun and distorting it. Here is the code, might you be able to point out what's wrong? This code's a compilation of a few other answers and solutions found around the forum, by the way.
var walkSpeed: float = 7; // regular speed var crchSpeed: float = 3; // crouching speed var runSpeed: float = 20; // run speed var walkFOV: float = 60; var runFOV: float = 65; var current: float = 0; private var ch$$anonymous$$otor: Character$$anonymous$$otor; private var tr: Transform; private var dist: float; // distance to ground
function Start(){ ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground }
function Update(){
var vScale = 1.0;
var speed = walkSpeed;
if (ch$$anonymous$$otor.isGrounded && Input.Get$$anonymous$$ey("left shift") || Input.Get$$anonymous$$ey("right shift") || Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)){
speed = runSpeed;
current = 1;
}
else
{
speed=walkSpeed;
current = 0;
}
if (Input.Get$$anonymous$$ey("c")){ // press C to crouch
vScale = 0.5;
speed = crchSpeed; // slow down when crouching
current=-1;
}
else
{
vScale=1.5;
}
if (current == 1)
{
if (Camera.main.fieldOfView < runFOV)
{
Camera.main.fieldOfView=Camera.main.fieldOfView+1.5;
}
}
else
{
if (Camera.main.fieldOfView > walkFOV)
{
Camera.main.fieldOfView=Camera.main.fieldOfView-1.5;
}
}
ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
var ultScale = tr.localScale.y; // crouch/stand up smoothly
tr.localScale.y = $$anonymous$$athf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}
One thing I notice is that you use else { if.
"else if" can be written as such, and in this particular case it should be just "else" ... ( if Camera.main.fieldOfView was equal walkFOV it would do nothing at all)
Try organizing your script with more comments for future notice, but for now just post a new question regarding the details, since I wouldn't personally know where to begin.