Search Unity

FPS Controller flying off in the air?

Discussion in 'Physics' started by Enoai, Apr 24, 2017.

  1. Enoai

    Enoai

    Joined:
    Apr 24, 2017
    Posts:
    3
    I'm using two combination of scripts for this controller, but first of all the problem.

    The problem is when I look all the way down at the floor and move backwards the controller flys up into the air, this also applies when I look up and walk forwards, how cna this be fixed?

    GIF OF PROBLEM: https://gyazo.com/f670b140aedc2e15c730364bc6b65c41

    Walking Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour {
    5.     public float defaultmoveSpeed = 8.00f;
    6.     public float moveSpeed = 8.00f;
    7.     public float jumpSpeed = 15.00f;
    8.     public float gravity = 20.0f;
    9.     public float sprintSpeed = 16.00f;
    10.     private Vector3 moveDirection = Vector3.zero;
    11.     private CharacterController playerController;
    12.     // Use this for initialization
    13.     void Start () {
    14.         playerController = GetComponent<CharacterController>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         //Movement Section
    20.         playerMovement();
    21.         //methods to which should only be used when player is grounded
    22.         if (playerController.isGrounded)
    23.         {
    24.             jumpMethod();
    25.         }
    26.    
    27.     }
    28.     public void jumpMethod()
    29.     {
    30.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    31.         moveDirection = transform.TransformDirection(moveDirection);
    32.         moveDirection *= moveSpeed;
    33.         if (Input.GetButtonDown("Jump"))
    34.             moveDirection.y = jumpSpeed;
    35.     }
    36.     public void playerMovement()
    37.     {
    38.         moveDirection.y -= gravity * Time.deltaTime;
    39.         playerController.Move(moveDirection * Time.deltaTime);
    40.         if (Input.GetKey("w"))
    41.         {
    42.             transform.Translate((Vector3.forward) * moveSpeed * Time.deltaTime);
    43.         }
    44.         if (Input.GetKey("S"))
    45.         {
    46.             transform.Translate((Vector3.back) * moveSpeed * Time.deltaTime);
    47.         }
    48.         if (Input.GetKey("A"))
    49.         {
    50.             transform.Translate((Vector3.left) * moveSpeed * Time.deltaTime);
    51.         }
    52.         if (Input.GetKey("D"))
    53.         {
    54.             transform.Translate((Vector3.right) * moveSpeed * Time.deltaTime);
    55.         }
    56.         // Sprint key function
    57.         if (Input.GetKey("left shift") && Input.GetKey("w"))
    58.         {
    59.             moveSpeed = sprintSpeed;
    60.         }
    61.         else
    62.         {
    63.             moveSpeed = defaultmoveSpeed;
    64.         }
    65.     }
    66. }

    Looking around Script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // Very simple smooth mouselook modifier for the MainCamera in Unity
    4. // by Francis R. Griffiths-Keam - www.runningdimensions.com
    5.  
    6. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    7. public class SimpleSmoothMouseLook : MonoBehaviour
    8. {
    9.     Vector2 _mouseAbsolute;
    10.     Vector2 _smoothMouse;
    11.  
    12.     public Vector2 clampInDegrees = new Vector2(360, 180);
    13.     public bool lockCursor;
    14.     public Vector2 sensitivity = new Vector2(2, 2);
    15.     public Vector2 smoothing = new Vector2(3, 3);
    16.     public Vector2 targetDirection;
    17.     public Vector2 targetCharacterDirection;
    18.  
    19.     // Assign this if there's a parent object controlling motion, such as a Character Controller.
    20.     // Yaw rotation will affect this object instead of the camera if set.
    21.     public GameObject characterBody;
    22.  
    23.     void Start()
    24.     {
    25.         // Set target direction to the camera's initial orientation.
    26.         targetDirection = transform.localRotation.eulerAngles;
    27.  
    28.         // Set target direction for the character body to its inital state.
    29.         if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         // Ensure the cursor is always locked when set
    35.         Screen.lockCursor = lockCursor;
    36.  
    37.         // Allow the script to clamp based on a desired target value.
    38.         var targetOrientation = Quaternion.Euler(targetDirection);
    39.         var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
    40.  
    41.         // Get raw mouse input for a cleaner reading on more sensitive mice.
    42.         var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    43.  
    44.         // Scale input against the sensitivity setting and multiply that against the smoothing value.
    45.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    46.  
    47.         // Interpolate mouse movement over time to apply smoothing delta.
    48.         _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    49.         _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    50.  
    51.         // Find the absolute mouse movement value from point zero.
    52.         _mouseAbsolute += _smoothMouse;
    53.  
    54.         // Clamp and apply the local x value first, so as not to be affected by world transforms.
    55.         if (clampInDegrees.x < 360)
    56.             _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
    57.  
    58.         var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
    59.         transform.localRotation = xRotation;
    60.  
    61.         // Then clamp and apply the global y value.
    62.         if (clampInDegrees.y < 360)
    63.             _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
    64.  
    65.         transform.localRotation *= targetOrientation;
    66.  
    67.         // If there's a character body that acts as a parent to the camera
    68.         if (characterBody)
    69.         {
    70.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
    71.             characterBody.transform.localRotation = yRotation;
    72.             characterBody.transform.localRotation *= targetCharacterOrientation;
    73.         }
    74.         else
    75.         {
    76.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    77.             transform.localRotation *= yRotation;
    78.         }
    79.     }
    80. }
    81.  
     
    Last edited: Apr 24, 2017