Advertisement
Redxone

Unity - Space Ship Code

Aug 15th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerMovement : MonoBehaviour {
  5.  
  6.     Animator anim;
  7.  
  8.     public float maxSpeed = 5f;
  9.     public float rotSpeed = 180f;
  10.     float sensitivity;
  11.     float shipBoundRadius = 0.5f;
  12.     bool isMoveing = false;
  13.    
  14.  
  15.     // Use this for initialization
  16.     void Start () {
  17.        
  18.         anim = GetComponent<Animator> ();
  19.  
  20.     }
  21.    
  22.     // Update is called once per frame
  23.     void Update () {
  24.  
  25.         //ROTATE Ship
  26.  
  27.         // Get Rotation Quaternion
  28.         Quaternion rotation = transform.rotation;
  29.  
  30.         // Grab Z Euler Angle
  31.         float z = rotation.eulerAngles.z;
  32.  
  33.         // Change Angle When Inputed
  34.         z -= Input.GetAxis ("Horizontal") * rotSpeed * Time.deltaTime;
  35.  
  36.         // Make a new Quaternion with X = 0, Y = 0, Z = Our Angle
  37.         rotation = Quaternion.Euler (0, 0, z);
  38.  
  39.         //Set rotation
  40.         transform.rotation = rotation;
  41.  
  42.  
  43.         //Input.GetAxis - 0 if not 0.num to 1 if forward -num if back
  44.         //Transform is pos rotatation and scale
  45.  
  46.         Vector3 pos = transform.position;
  47.         Vector3 velocity = new Vector3( 0,  Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime,  0);
  48.  
  49.         float screenRatio = (float) Screen.width / (float) Screen.height;
  50.         float mWidth = Camera.main.orthographicSize * screenRatio;
  51.  
  52.         if (pos.y > (Camera.main.orthographicSize+shipBoundRadius)) {
  53.             pos.y = -Camera.main.orthographicSize;
  54.         }
  55.         else if (pos.y < -(Camera.main.orthographicSize+shipBoundRadius)) {
  56.             pos.y = Camera.main.orthographicSize;
  57.         }
  58.    
  59.         if (pos.x > (mWidth+shipBoundRadius)) {
  60.             pos.x = -mWidth;
  61.         }
  62.         else if (pos.x < -(mWidth+shipBoundRadius)) {
  63.             pos.x = mWidth;
  64.         }
  65.  
  66.         pos += rotation * velocity;
  67.  
  68.         transform.position = pos;
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement