Guest User

Untitled

a guest
Jan 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 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.     float walkingSpeed = 24;
  13.     float runningSpeed = 42;
  14.     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.     public Vector3 acceleration = new Vector3 (1.0f, 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.GetKey(moveLeft)) {
  41.             print("A Pressed");
  42.             movementState = 1;
  43.            
  44.         }
  45.        
  46.     }
  47.    
  48.     // FixedUpdate is called once per physics step
  49.     void FixedUpdate () {
  50.         float maxSpeedSqr = movementState==1?walkingSpeedSqr:
  51.                       movementState==2?runningSpeedSqr:
  52.                       movementState==3?sprintSpeedSqr:0;
  53.  
  54.         //Makes sure player is moving
  55.         if (movementState>0) {
  56.             print("movementState > 0");
  57.             if (rigidbody.velocity.sqrMagnitude<maxSpeedSqr) {
  58.                 print("rigidbody.velocity.sqrMagnitude < maxSpeedSqr");
  59.                 rigidbody.AddForce(acceleration);
  60.             }
  61.         }
  62.    
  63.     }
  64.  
  65. }
Add Comment
Please, Sign In to add comment