Advertisement
Guest User

Unity 3rd person spaceship

a guest
Jan 24th, 2013
8,974
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.     //speed stuff
  7.     float speed;
  8.     public int cruiseSpeed;
  9.     float deltaSpeed;//(speed - cruisespeed)
  10.     public int minSpeed;
  11.     public int maxSpeed;
  12.     float accel, decel;
  13.  
  14.     //turning stuff
  15.     Vector3 angVel;
  16.     Vector3 shipRot;
  17.     public int sensitivity;
  18.  
  19.     public Vector3 cameraOffset; //I use (0,1,-3)
  20.  
  21.     void Start()
  22.     {
  23.         speed = cruiseSpeed;
  24.     }
  25.  
  26.     void FixedUpdate()
  27.     {
  28.  
  29.         //ANGULAR DYNAMICS//
  30.        
  31.         shipRot = transform.GetChild(1).localEulerAngles; //make sure you're getting the right child (the ship).  I don't know how they're numbered in general.
  32.  
  33.         //since angles are only stored (0,360), convert to +- 180
  34.         if (shipRot.x > 180) shipRot.x -= 360;
  35.         if (shipRot.y > 180) shipRot.y -= 360;
  36.         if (shipRot.z > 180) shipRot.z -= 360;
  37.  
  38.         //vertical stick adds to the pitch velocity
  39.         //         (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
  40.         angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
  41.  
  42.         //horizontal stick adds to the roll and yaw velocity... also thanks to the .5 you can't turn as fast/far sideways as you can pull up/down
  43.         float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
  44.         angVel.y += turn * .5f;
  45.         angVel.z -= turn * .5f;
  46.  
  47.  
  48.         //shoulder buttons add to the roll and yaw.  No deltatime here for a quick response
  49.         //comment out the .y parts if you don't want to turn when you hit them
  50.         if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
  51.         {
  52.             angVel.y -= 20;
  53.             angVel.z += 50;
  54.             speed -= 5 * Time.fixedDeltaTime;
  55.         }
  56.  
  57.         if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
  58.         {
  59.             angVel.y += 20;
  60.             angVel.z -= 50;
  61.             speed -= 5 * Time.fixedDeltaTime;
  62.         }
  63.  
  64.  
  65.         //your angular velocity is higher when going slower, and vice versa.  There probably exists a better function for this.
  66.         angVel /= 1 + deltaSpeed * .001f;
  67.  
  68.         //this is what limits your angular velocity.  Basically hard limits it at some value due to the square magnitude, you can
  69.         //tweak where that value is based on the coefficient
  70.         angVel -= angVel.normalized * angVel.sqrMagnitude * .08f * Time.fixedDeltaTime;
  71.  
  72.  
  73.         //and finally rotate.  
  74.         transform.GetChild(1).Rotate(angVel * Time.fixedDeltaTime);
  75.  
  76.         //this limits your rotation, as well as gradually realigns you.  It's a little convoluted, but it's
  77.         //got the same square magnitude functionality as the angular velocity, plus a constant since x^2
  78.         //is very small when x is small.  Also realigns faster based on speed.  feel free to tweak
  79.         transform.GetChild(1).Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
  80.  
  81.  
  82.         //LINEAR DYNAMICS//
  83.  
  84.         deltaSpeed = speed - cruiseSpeed;
  85.  
  86.         //This, I think, is a nice way of limiting your speed.  Your acceleration goes to zero as you approach the min/max speeds, and you initially
  87.         //brake and accelerate a lot faster.  Could potentially do the same thing with the angular stuff.
  88.         decel = speed - minSpeed;
  89.         accel = maxSpeed - speed;
  90.  
  91.         //simple accelerations
  92.         if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
  93.             speed += accel * Time.fixedDeltaTime;
  94.         else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
  95.             speed -= decel * Time.fixedDeltaTime;
  96.  
  97.         //if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
  98.         //(added clamping since it's more of a gradual slowdown/speedup)
  99.         else if (Mathf.Abs(deltaSpeed) > .1f)
  100.             speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
  101.  
  102.  
  103.         //moves camera (make sure you're GetChild()ing the camera's index)
  104.         //I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
  105.         transform.GetChild(0).localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
  106.  
  107.  
  108.         float sqrOffset = transform.GetChild(1).localPosition.sqrMagnitude;
  109.         Vector3 offsetDir = transform.GetChild(1).localPosition.normalized;
  110.  
  111.  
  112.         //this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
  113.         //I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
  114.         transform.GetChild(1).Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
  115.                                                        //(**************** this ***************) is what actually makes the whole ship move through the world!
  116.         transform.Translate((offsetDir * sqrOffset * 50 + transform.GetChild(1).forward * speed) * Time.fixedDeltaTime, Space.World);
  117.  
  118.         //comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
  119.         transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
  120.     }
  121.  
  122.     void Update()
  123.     {
  124.     }
  125. }
Advertisement
RAW Paste Data Copied
Advertisement