- Home /
Gun stretches and moves around to much in FPS
Hello, Im having trouble with my FPS. I have attahced my gun the the camera which is attached to the Controller. However, when this is done, the gun doesn't move at the same speed as the camera, it goes off and stretches when I look up or down. Ive written my own player movement code. Any ideas? Please be detailed, i am new to this and there are no answers when this question has been asked before. Here are two examples
Here is my own player movement code attached to the Player controller. I have not used any drag and drop movements or scripts for this.
//VARIABLES FOR SOUND
public AudioClip jump;
public AudioClip collision;
public AudioSource playerSpeaker;
//VARIABLES AND JUNK!
private float mouseSpeed = 5.0f;
private float verticalRot = 0;
private float upAndDown = 60.0f;
private float haulAssSpeed = 10.0f;
private float verticalSpeed = 0f;
private float earthGravy = 5.0f;
private float initialJumpVelocity = 20.0f;
//BOOLS
private bool jumping = true;
static bool gameOn = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (gameOn == true)
{
CharacterController player1 = GetComponent<CharacterController>();
//look left and right
float rotationLR = Input.GetAxis("Mouse X") * mouseSpeed;
transform.Rotate(0, rotationLR, 0);
//Look up and down
verticalRot -= Input.GetAxis("Mouse Y") * mouseSpeed;
float desiredUpDown = verticalRot;
//stop from spining around by looking up and down
desiredUpDown = Mathf.Clamp(desiredUpDown, -upAndDown, upAndDown);
Camera.main.transform.localRotation = Quaternion.Euler(desiredUpDown, 0, 0);
//Forward and side movements
float fwdSpeed = Input.GetAxis("Vertical") * haulAssSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * haulAssSpeed;
//Earths Gravy
verticalSpeed += Physics.gravity.y * Time.deltaTime * earthGravy;
//jumping
if (Input.GetButtonDown("Jump") && player1.isGrounded)
{
verticalSpeed = initialJumpVelocity;
}
//Create a vector, each of the three speeds, move our character by that vector!
Vector3 speed = new Vector3(sideSpeed, verticalSpeed, fwdSpeed);
speed = transform.rotation * speed;
//avoid glicthy play
player1.Move(speed * Time.deltaTime);
}
}
public static void playGameNow()
{
gameOn = true;
}
public static void stopGameNow()
{
gameOn = false;
}
}
If you have done this correctly, what did you do? is it settings with controller or the camera? Any help greatly appreciated.
Thanks Again!
Hey, i have the same problem! I just cant find the solution. I followed qill18creates tutorials on youtube and everything is working fine, just the gun is acting wierd.
Answer by Mihaaa_123_123 · Apr 08, 2016 at 07:18 PM
I found the solution! It's very simple actually.
Use your object with player controller component and your script as the main parent. Create an empty object and attach your camera to it. (player2 is my empty object and the camera is attached to it)
And now put your player2 under your player.
Answer by darkgamerxt · Sep 09, 2019 at 11:40 AM
had same issue,im new my self, what i did i created capsule and removed everything from it except for character controller, i put my playermove script in the capsule, i made the capsule parent for both the playercamera and the playerbody , i had my mouse look script in camera, so that way when the capsule moves the player body fellow, and when the camera rotate it rotate based on a capsule which removes the glitch of gun stretching, should work
Answer by MikeDockery · Aug 08, 2018 at 10:31 AM
I had this problem and i dropped a new 1st person controller into the scene, copy over all the setting and scripts from the old one and it worked. Not sure why it was happening but like all these situations you either spend days figuring it out or get it solved and move on with game creation.