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
- float walkingSpeed = 24;
- float runningSpeed = 42;
- 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
- public Vector3 acceleration = new Vector3 (1.0f, 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.GetKey(moveLeft)) {
- print("A Pressed");
- movementState = 1;
- }
- }
- // 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) {
- print("movementState > 0");
- if (rigidbody.velocity.sqrMagnitude<maxSpeedSqr) {
- print("rigidbody.velocity.sqrMagnitude < maxSpeedSqr");
- rigidbody.AddForce(acceleration);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment