Search Unity

Improving a FP Controller

Discussion in 'Scripting' started by cgprojects, Sep 9, 2017.

  1. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46
    Hello,

    Right now I have a script that allows movement with the addition of the built in Character Controller. I would rather use one code. The advantage with the built in controller it allows moving upwards like slope. I would like to advance mine with that ability and proper physics movement like the built in character controller. Additionally I would like to add the ability to do light thrust based movement that can be enabled and disabled.

    Any suggestions?

    My current script

    Code (CSharp):
    1. // Import
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. using UnityStandardAssets.CrossPlatformInput;
    7. using UnityStandardAssets.ImageEffects;
    8.  
    9. // First Person Controller
    10. public class FPController : MonoBehaviour
    11. {
    12.     // Variables
    13.     private CharacterController m_pCharacterController = null;
    14.     private Transform m_pTransform;
    15.     private Vector3 m_MoveDirection;
    16.     public  float m_SpeedMuiltipler;
    17.     public Camera m_MainCamera;
    18.     public float m_MouseSensitivityX;
    19.     public float m_MouseSensitivityY;
    20.     private float m_VerticalVelocity;
    21.     private float m_VerticalRotation;
    22.     private float rotLeftRight;
    23.  
    24.     public Transform m_PlaceHolder;
    25.  
    26.     public bool m_UseDefaultInput;
    27.     public bool m_Climbing;
    28.  
    29.     public Vector3 m_Offset;
    30.  
    31.     private Player m_Player;
    32.     private bool m_WindowActive;
    33.  
    34.  
    35.     public VignetteAndChromaticAberration m_Vignette;
    36.  
    37.     // Use this for initialization
    38.     void Start ()
    39.     {
    40.         // is climbing
    41.         m_Climbing = false;
    42.  
    43.         // Forces Character to Null
    44.         m_pCharacterController = null;
    45.         m_Player = null;
    46.  
    47.         // Get Character Controller
    48.         m_pCharacterController = this.GetComponent<CharacterController> ();
    49.  
    50.         // Get Transform
    51.         m_pTransform = transform;
    52.  
    53.         // Set velocity
    54.         m_VerticalVelocity = Physics.gravity.y;
    55.  
    56.         // Enable Screen lock
    57.         if (m_UseDefaultInput == false) {
    58.             //    Screen.lockCursor = true;
    59.         }
    60.  
    61.         // Get Player
    62.         m_Player = GlobalVariables.gm.GetPlayer ();
    63.  
    64.         // Set window to active
    65.         m_WindowActive = false;
    66.     }
    67.    
    68.     // Update is called once per frame
    69.     void Update ()
    70.     {
    71.         // If no unity character controller then exit
    72.         if (!m_pCharacterController) {
    73.             return;
    74.         }
    75.  
    76.         // if a window is active close
    77.         if (m_WindowActive == true)
    78.             return;      
    79.  
    80.         // Get Mouse Rotation - Handle Horizontal
    81.         if (m_UseDefaultInput) {
    82.             rotLeftRight = Input.GetAxis ("Mouse X") * m_MouseSensitivityX;
    83.             m_VerticalRotation -= Input.GetAxis ("Mouse Y") * m_MouseSensitivityY;
    84.         } else {
    85.             rotLeftRight = CrossPlatformInputManager.GetAxis ("Mouse X") * m_MouseSensitivityX;
    86.             m_VerticalRotation -= CrossPlatformInputManager.GetAxis ("Mouse Y") * m_MouseSensitivityY;
    87.         }
    88.  
    89.  
    90.         // Get Mouse Rotation - Handle Vertical
    91.         m_VerticalRotation = Mathf.Clamp (m_VerticalRotation, -40, 40);
    92.  
    93.         // Rotate Left and Right
    94.         m_pTransform.Rotate (0, rotLeftRight, 0);
    95.  
    96.         //m_MainCamera.transform.localRotation = Quaternion.Euler (m_VerticalRotation, 0, 0);
    97.         // Use the place holder
    98.         m_PlaceHolder.localRotation = Quaternion.Euler (m_VerticalRotation, 0, 0);
    99.  
    100.         // Get movement on the side
    101.         float sideSpeed = 0;
    102.         float forwardSpeed = 0;
    103.  
    104.         // use default movemt for side this should WAFD
    105.         if (m_UseDefaultInput) {
    106.             sideSpeed = Input.GetAxis ("Horizontal");
    107.             forwardSpeed = Input.GetAxis ("Vertical");
    108.  
    109.         } else {
    110.             sideSpeed = CrossPlatformInputManager.GetAxis ("Horizontal");
    111.             forwardSpeed = CrossPlatformInputManager.GetAxis ("Vertical");
    112.         }
    113.            
    114.         // Create Movement speed
    115.         Vector3 moveSpeed = new Vector3 (sideSpeed * m_SpeedMuiltipler, m_VerticalVelocity, forwardSpeed * m_SpeedMuiltipler);
    116.  
    117.         // Multiple speed with rotation
    118.         moveSpeed = transform.rotation * moveSpeed;
    119.  
    120.         // if Clibing then reset
    121.         if (m_Climbing == true) {
    122.             moveSpeed.x = 0;
    123.             moveSpeed.z = 0;
    124.             moveSpeed.y = 0;
    125.  
    126.             if (Input.GetKeyDown (KeyCode.UpArrow) || forwardSpeed > 0) {
    127.                 moveSpeed.y = 0.8f;
    128.             }
    129.  
    130.             if (Input.GetKeyDown (KeyCode.DownArrow) || forwardSpeed < 0) {
    131.                 moveSpeed.y = -0.8f;
    132.                            
    133.             }
    134.                
    135.             if (Input.GetKeyDown (KeyCode.J) || CrossPlatformInputManager.GetButton ("Jump")) {
    136.                 m_Climbing = false;                      
    137.             }
    138.  
    139.             if (m_Player != null) {
    140.                 m_Player.DecreaseHealth (1);
    141.             }
    142.         }
    143.  
    144.         // if Jump is clicked
    145.         if (m_pCharacterController.isGrounded && CrossPlatformInputManager.GetButton ("Jump") && m_Climbing == false) {
    146.             m_VerticalVelocity = 5.0f;
    147.         }
    148.  
    149.         // Calculate Vertical Velocity
    150.         if (m_Climbing == false) {
    151.             m_VerticalVelocity += Physics.gravity.y * Time.deltaTime;
    152.         }
    153.  
    154.         // Move based on deltatime
    155.         m_pCharacterController.Move (moveSpeed * Time.deltaTime);
    156.  
    157.         // Decrease health if move
    158.         if (sideSpeed != 0 || forwardSpeed != 0) {
    159.             m_Player.DecreaseHealth (1);
    160.         }
    161.     }
    162.  
    163.     public void SetClimbing (bool state, Vector3 offset)
    164.     {
    165.         // set climbing and offset
    166.         m_Climbing = state;
    167.         m_Offset = offset;
    168.     }
    169.  
    170.     public void SetWindowActive (bool active)
    171.     {
    172.         // set window active
    173.         m_WindowActive = active;
    174.     }
    175.  
    176.     public void LockPlayer ()
    177.     {
    178.         // If no unity character controller then exit
    179.         m_pCharacterController = null;
    180.     }
    181. }
    182.  
    Vivienne
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Some small tips:
    - You don't need to set m_pCharacterController to null on line 44 because it's already null an dyou assign it anyway on 48.
    - GetComponent can be called without the "this" prefix. It just refers to the object the code is in.
    - You don't need "if(condition == true)", because the == operator returns a boolean true or false anyway. You can just do if(condition). For "== false" just use "if(!condition)".
    - Physics.gravity is an acceleration, not a velocity. Velocity = ΔPosition, Acceleration = ΔVelocity.

    The character controller scripts just use Rigidbody.Move. They're also open source so you can view the code in them for reference like any other script.
     
  3. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46
    I rewrote half the script which I have not tried testing to mobile. I would look at your comments and additionally the RigidBody.Move open source. Eventually I want to use one controller code instead of two controllers.