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): // Import using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; using UnityStandardAssets.ImageEffects; // First Person Controller public class FPController : MonoBehaviour { // Variables private CharacterController m_pCharacterController = null; private Transform m_pTransform; private Vector3 m_MoveDirection; public float m_SpeedMuiltipler; public Camera m_MainCamera; public float m_MouseSensitivityX; public float m_MouseSensitivityY; private float m_VerticalVelocity; private float m_VerticalRotation; private float rotLeftRight; public Transform m_PlaceHolder; public bool m_UseDefaultInput; public bool m_Climbing; public Vector3 m_Offset; private Player m_Player; private bool m_WindowActive; public VignetteAndChromaticAberration m_Vignette; // Use this for initialization void Start () { // is climbing m_Climbing = false; // Forces Character to Null m_pCharacterController = null; m_Player = null; // Get Character Controller m_pCharacterController = this.GetComponent<CharacterController> (); // Get Transform m_pTransform = transform; // Set velocity m_VerticalVelocity = Physics.gravity.y; // Enable Screen lock if (m_UseDefaultInput == false) { // Screen.lockCursor = true; } // Get Player m_Player = GlobalVariables.gm.GetPlayer (); // Set window to active m_WindowActive = false; } // Update is called once per frame void Update () { // If no unity character controller then exit if (!m_pCharacterController) { return; } // if a window is active close if (m_WindowActive == true) return; // Get Mouse Rotation - Handle Horizontal if (m_UseDefaultInput) { rotLeftRight = Input.GetAxis ("Mouse X") * m_MouseSensitivityX; m_VerticalRotation -= Input.GetAxis ("Mouse Y") * m_MouseSensitivityY; } else { rotLeftRight = CrossPlatformInputManager.GetAxis ("Mouse X") * m_MouseSensitivityX; m_VerticalRotation -= CrossPlatformInputManager.GetAxis ("Mouse Y") * m_MouseSensitivityY; } // Get Mouse Rotation - Handle Vertical m_VerticalRotation = Mathf.Clamp (m_VerticalRotation, -40, 40); // Rotate Left and Right m_pTransform.Rotate (0, rotLeftRight, 0); //m_MainCamera.transform.localRotation = Quaternion.Euler (m_VerticalRotation, 0, 0); // Use the place holder m_PlaceHolder.localRotation = Quaternion.Euler (m_VerticalRotation, 0, 0); // Get movement on the side float sideSpeed = 0; float forwardSpeed = 0; // use default movemt for side this should WAFD if (m_UseDefaultInput) { sideSpeed = Input.GetAxis ("Horizontal"); forwardSpeed = Input.GetAxis ("Vertical"); } else { sideSpeed = CrossPlatformInputManager.GetAxis ("Horizontal"); forwardSpeed = CrossPlatformInputManager.GetAxis ("Vertical"); } // Create Movement speed Vector3 moveSpeed = new Vector3 (sideSpeed * m_SpeedMuiltipler, m_VerticalVelocity, forwardSpeed * m_SpeedMuiltipler); // Multiple speed with rotation moveSpeed = transform.rotation * moveSpeed; // if Clibing then reset if (m_Climbing == true) { moveSpeed.x = 0; moveSpeed.z = 0; moveSpeed.y = 0; if (Input.GetKeyDown (KeyCode.UpArrow) || forwardSpeed > 0) { moveSpeed.y = 0.8f; } if (Input.GetKeyDown (KeyCode.DownArrow) || forwardSpeed < 0) { moveSpeed.y = -0.8f; } if (Input.GetKeyDown (KeyCode.J) || CrossPlatformInputManager.GetButton ("Jump")) { m_Climbing = false; } if (m_Player != null) { m_Player.DecreaseHealth (1); } } // if Jump is clicked if (m_pCharacterController.isGrounded && CrossPlatformInputManager.GetButton ("Jump") && m_Climbing == false) { m_VerticalVelocity = 5.0f; } // Calculate Vertical Velocity if (m_Climbing == false) { m_VerticalVelocity += Physics.gravity.y * Time.deltaTime; } // Move based on deltatime m_pCharacterController.Move (moveSpeed * Time.deltaTime); // Decrease health if move if (sideSpeed != 0 || forwardSpeed != 0) { m_Player.DecreaseHealth (1); } } public void SetClimbing (bool state, Vector3 offset) { // set climbing and offset m_Climbing = state; m_Offset = offset; } public void SetWindowActive (bool active) { // set window active m_WindowActive = active; } public void LockPlayer () { // If no unity character controller then exit m_pCharacterController = null; } } Vivienne
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.
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.