using System.Collections; using System.Collections.Generic; using UnityEngine; // TODO: Import Standard assets CrossPlatformInput library using UnityStandardAssets.CrossPlatformInput; public class PlayerMovementKeyboard : MonoBehaviour, IPlayerMovement { protected CharacterController characterController; void Start() { // Get and cache the character controller component attached to the game object this script is attached to characterController = GetComponent(); } // See more: https://docs.unity3d.com/ScriptReference/CharacterController.Move.html public Vector3 GetMovement(float walkSpeed, float runSpeedMultiplier, float rotateSpeed) { // read inputs on horizontal (h) and vertical (v) float h = CrossPlatformInputManager.GetAxis("Horizontal"); float v = CrossPlatformInputManager.GetAxis("Vertical"); // Returns the movement vector Vector3 movement = new Vector3(h, 0f, v); // Apply speed movement *= walkSpeed; //// Apply jumping if applicable // If there is movement, i.e. movement is not zero, if (movement != Vector3.zero) { // If this is not a mobile device, if user is running (pressing left shift key) increase speed by multiplying by run speed multiplier #if !MOBILE_INPUT if (Input.GetKey(KeyCode.LeftShift)) { } #endif // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below // when the movement is multiplied by deltaTime). This is because gravity should be applied // as an acceleration (ms^-2) //If is jumping apply gravity // Gradually rotate character forward in direction of the movement direction (Quaternion.LookRotation) // using the defined rotation speed and make it frame rate independent (multiply by Time.deltatime) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), Time.deltaTime * rotateSpeed); // make it frame rate independent movement *= Time.deltaTime; characterController.Move(movement); } return movement; } }