Guest User

Untitled

a guest
Jan 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Controls : MonoBehaviour {
  5.    
  6.     public KeyCode moveLeft;
  7.     public KeyCode moveRight;
  8.     public KeyCode moveForward;
  9.     public KeyCode moveBackward;
  10.    
  11.     //Speeds for different states
  12.     public float walkingSpeed = 24;
  13.     public float runningSpeed = 42;
  14.     public float sprintSpeed = 55;
  15.    
  16.     //
  17.     public float walkingSpeedSqr;
  18.     public float runningSpeedSqr;
  19.     public float sprintSpeedSqr;
  20.  
  21.     public int movementState = 0;  // 0 = not moving, 1 = walking, 2 = running, 3 = sprinting
  22.    
  23.     Vector3 acceleration = new Vector3 (1.0F*Time.deltaTime, 0, 0);
  24.    
  25.     // Use this for initialization
  26.     void Start () {
  27.         moveLeft = KeyCode.A;
  28.         moveRight = KeyCode.D;
  29.         moveForward = KeyCode.W;
  30.         moveBackward = KeyCode.S;
  31.        
  32.         walkingSpeedSqr = walkingSpeed*walkingSpeed ;
  33.         runningSpeedSqr = runningSpeed*runningSpeed;
  34.         sprintSpeedSqr = sprintSpeed*sprintSpeed;
  35.        
  36.     }
  37.    
  38.     // Update is called once per frame
  39.     void Update () {
  40.         if(Input.GetKeyDown(moveLeft)) {
  41.            
  42.         }
  43.        
  44.     }
  45.    
  46.     // FixedUpdate is called once per physics step
  47.     void FixedUpdate () {
  48.         float maxSpeedSqr = movementState==1?walkingSpeedSqr:
  49.                       movementState==2?runningSpeedSqr:
  50.                       movementState==3?sprintSpeedSqr:0;
  51.  
  52.         //Makes sure player is moving
  53.         if (movementState>0) {
  54.             if (rigidbody.velocity.sqrMagnitude>maxSpeedSqr) {
  55.                 rigidbody.AddForce(acceleration);
  56.             }
  57.         }
  58.    
  59.     }
  60.  
  61. }
Add Comment
Please, Sign In to add comment