Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Controls : MonoBehaviour {
- public KeyCode moveLeft;
- public KeyCode moveRight;
- public KeyCode moveForward;
- public KeyCode moveBackward;
- //Speeds for different states
- public float walkingSpeed = 24;
- public float runningSpeed = 42;
- public float sprintSpeed = 55;
- //
- public float walkingSpeedSqr;
- public float runningSpeedSqr;
- public float sprintSpeedSqr;
- public int movementState = 0; // 0 = not moving, 1 = walking, 2 = running, 3 = sprinting
- Vector3 acceleration = new Vector3 (1.0F*Time.deltaTime, 0, 0);
- // Use this for initialization
- void Start () {
- moveLeft = KeyCode.A;
- moveRight = KeyCode.D;
- moveForward = KeyCode.W;
- moveBackward = KeyCode.S;
- walkingSpeedSqr = walkingSpeed*walkingSpeed ;
- runningSpeedSqr = runningSpeed*runningSpeed;
- sprintSpeedSqr = sprintSpeed*sprintSpeed;
- }
- // Update is called once per frame
- void Update () {
- if(Input.GetKeyDown(moveLeft)) {
- }
- }
- // FixedUpdate is called once per physics step
- void FixedUpdate () {
- float maxSpeedSqr = movementState==1?walkingSpeedSqr:
- movementState==2?runningSpeedSqr:
- movementState==3?sprintSpeedSqr:0;
- //Makes sure player is moving
- if (movementState>0) {
- if (rigidbody.velocity.sqrMagnitude>maxSpeedSqr) {
- rigidbody.AddForce(acceleration);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment