Wolfos

Untitled

Dec 5th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Rocket : MonoBehaviour {
  5.     public Transform[] planets;
  6.     public float speed = 20;
  7.     public float rotspeed = 1;
  8.     private Vector2 gravity = new Vector2(0, 0);
  9.  
  10.     void FixedUpdate ()
  11.     {
  12.         //Gravity
  13.         foreach (Transform planet in planets)
  14.         {
  15.             Vector2 pos = new Vector2(transform.position.x, transform.position.y);
  16.             Vector2 planetpos = new Vector2(planet.position.x, planet.position.y);
  17.             gravity = planet.GetComponent<Planet>().mass * (planetpos - pos) / (Distance(pos, planetpos) * Distance(pos, planetpos));
  18.             rigidbody2D.AddForce(gravity);
  19.         }
  20.  
  21.         //Thrust
  22.         if (Input.GetAxis("Vertical") > 0)
  23.         {
  24.             rigidbody2D.AddForce(transform.up * Input.GetAxis("Vertical") * speed);
  25.         }
  26.         rigidbody2D.AddTorque(-Input.GetAxis("Horizontal") * rotspeed);
  27.     }
  28.  
  29.     float Distance(Vector2 pos1, Vector2 pos2)
  30.     {
  31.         float csquared = (pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y);
  32.         return Mathf.Sqrt(csquared);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment