Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpaceshipController : MonoBehaviour {
  6.  
  7.     public float rotationSpeed = 200f;
  8.     public float thrustForce = 5f;
  9.     private Rigidbody2D _rigidBody2d;
  10.  
  11.     public float maxLandVelocity = 1.0f;
  12.     public float minAligmnentCollissionRatio = 0.97f;
  13.  
  14.     void Start () {
  15.         _rigidBody2d = GetComponent<Rigidbody2D>();
  16.     }
  17.  
  18.     private void OnCollisionEnter2D(Collision2D collision) {
  19.         Vector2 colVector = collision.contacts[0].point - (Vector2)collision.collider.transform.position;
  20.         float alignmentCollisionRatio = Vector2.Dot(colVector.normalized, transform.rotation * Vector2.up);
  21.         if (_rigidBody2d.velocity.magnitude <= maxLandVelocity && alignmentCollisionRatio > minAligmnentCollissionRatio) {
  22.             _rigidBody2d.velocity = Vector3.zero;
  23.             _rigidBody2d.angularVelocity = 0.0f;
  24.             Debug.Log("Landed like a champ");
  25.         }
  26.         else {
  27.             //TODO: blow this bitch up
  28.             _rigidBody2d.velocity = Vector3.zero;
  29.             _rigidBody2d.angularVelocity = 0.0f;
  30.             transform.position = new Vector3(0, 0, 0);
  31.             Debug.Log("morreu jovem, volte pro meio do mapa");
  32.         }
  33.     }
  34.    
  35.     void Update () {
  36.         // Rotate if it's not landed
  37.         if (_rigidBody2d.velocity.magnitude > 0) {
  38.             transform.Rotate(0, 0, -Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime);
  39.         }
  40.  
  41.         // Thrust foward but never backwards
  42.         GetComponent<Rigidbody2D>().
  43.             AddForce(transform.up * thrustForce *
  44.                 Mathf.Max(0, Input.GetAxis("Vertical")));
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement