Advertisement
Arycama

Space movement script

May 29th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class playerControl : MonoBehaviour
  5. {
  6.     public float rotationSpeed;
  7.  
  8.     public float maxForwardAcceleration;
  9.     public float maxBackwardAcceleration;
  10.     public float maxStrafeAcceleration;
  11.     public float maxClimbAcceleration;
  12.     public float maxBoostAcceleration;
  13.  
  14.     public float forwardAcceleration;
  15.     public float boostAcceleration;
  16.     public float backwardAcceleration;
  17.     public float strafeAcceleration;
  18.     public float climbAcceleration;
  19.  
  20.     private float boost;
  21.     public float forward;
  22.     private float backward;
  23.     private float left;
  24.     private float right;
  25.     private float up;
  26.     private float down;
  27.  
  28.     void FixedUpdate()
  29.     {
  30.         float pitch = Input.GetAxis("Pitch") * rotationSpeed * Time.fixedDeltaTime;
  31.         float yaw = Input.GetAxis("Yaw") * rotationSpeed * Time.fixedDeltaTime;
  32.         float roll = Input.GetAxisRaw("Roll") * rotationSpeed * Time.fixedDeltaTime;
  33.  
  34.         //Move forward if under speed limit
  35.         if (forward < maxForwardAcceleration)
  36.         {
  37.             forward += Input.GetAxisRaw("Forward") * forwardAcceleration * Time.fixedDeltaTime;
  38.         }
  39.  
  40.         //Move backward if under speed limit
  41.         if (backward < maxBackwardAcceleration)
  42.         {
  43.             backward += Input.GetAxisRaw("Backward") * backwardAcceleration * Time.fixedDeltaTime;
  44.         }
  45.  
  46.         //Move left if under speed limit
  47.         if (left < maxStrafeAcceleration)
  48.         {
  49.             left += Input.GetAxisRaw("Left") * strafeAcceleration * Time.fixedDeltaTime;
  50.         }
  51.  
  52.         //Move right if under speed limit
  53.         if (right < maxStrafeAcceleration)
  54.         {
  55.             right += Input.GetAxisRaw("Right") * strafeAcceleration * Time.fixedDeltaTime;
  56.         }
  57.  
  58.         //Move up if under speed limit
  59.         if (up < maxStrafeAcceleration)
  60.         {
  61.             up += Input.GetAxisRaw("Up") * climbAcceleration * Time.fixedDeltaTime;
  62.         }
  63.  
  64.         //Move down if under speed limit
  65.         if (down < maxStrafeAcceleration)
  66.         {
  67.             down += Input.GetAxisRaw("Down") * climbAcceleration * Time.fixedDeltaTime;
  68.         }
  69.  
  70.         //Boost
  71.         if (boost < maxBoostAcceleration && Input.GetAxisRaw("Forward") > 0)
  72.         {
  73.             boost += Input.GetAxisRaw("Boost") * boostAcceleration * Time.fixedDeltaTime;
  74.         }
  75.  
  76.         //Slows if forward key is released
  77.         if (Input.GetAxisRaw("Forward") < 1 && forward > 0)
  78.         {
  79.             forward = 0;
  80.         }
  81.  
  82.         //Slows if backward key is released
  83.         if (Input.GetAxisRaw("Backward") < 1 && backward > 0)
  84.         {
  85.             backward = 0;
  86.         }
  87.  
  88.         //Slows if left key is released
  89.         if (Input.GetAxisRaw("Left") < 1 && left > 0)
  90.         {
  91.             left = 0;
  92.         }
  93.  
  94.         //Slows if right key is released
  95.         if (Input.GetAxisRaw("Right") < 1 && right > 0)
  96.         {
  97.             right = 0;
  98.         }
  99.  
  100.         //Slows if up key is released
  101.         if (Input.GetAxisRaw("Up") < 1 && up > 0)
  102.         {
  103.             up = 0;
  104.         }
  105.  
  106.         //Slows if down key is released
  107.         if (Input.GetAxisRaw("Down") < 1 && down > 0)
  108.         {
  109.             down = 0;
  110.         }
  111.  
  112.         //Slows if boost key is released
  113.         if (Input.GetAxisRaw("Boost") < 1 && boost > 0)
  114.         {
  115.             boost = 0;
  116.         }
  117.  
  118.         rigidbody.AddRelativeTorque(pitch, yaw, roll);
  119.  
  120.         rigidbody.AddRelativeForce(Vector3.forward * forward);
  121.         rigidbody.AddRelativeForce(Vector3.back * backward);
  122.         rigidbody.AddRelativeForce(Vector3.left * left);
  123.         rigidbody.AddRelativeForce(Vector3.right * right);
  124.         rigidbody.AddRelativeForce(Vector3.up * up);
  125.         rigidbody.AddRelativeForce(Vector3.down * down);
  126.         rigidbody.AddRelativeForce(Vector3.forward * boost);
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement