Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Rocket : MonoBehaviour {
- public Transform[] planets;
- public float speed = 20;
- public float rotspeed = 1;
- private Vector2 gravity = new Vector2(0, 0);
- void FixedUpdate ()
- {
- //Gravity
- foreach (Transform planet in planets)
- {
- Vector2 pos = new Vector2(transform.position.x, transform.position.y);
- Vector2 planetpos = new Vector2(planet.position.x, planet.position.y);
- gravity = planet.GetComponent<Planet>().mass * (planetpos - pos) / (Distance(pos, planetpos) * Distance(pos, planetpos));
- rigidbody2D.AddForce(gravity);
- }
- //Thrust
- if (Input.GetAxis("Vertical") > 0)
- {
- rigidbody2D.AddForce(transform.up * Input.GetAxis("Vertical") * speed);
- }
- rigidbody2D.AddTorque(-Input.GetAxis("Horizontal") * rotspeed);
- }
- float Distance(Vector2 pos1, Vector2 pos2)
- {
- float csquared = (pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y);
- return Mathf.Sqrt(csquared);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment