Advertisement
Teravisor

Unity+C# simple missile

Jan 24th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SimpleSteering : MonoBehaviour
  5. {
  6.     Rigidbody2D rb2D;
  7.     //Target we want to hit. Rigidbody2D because we can hit if it's moving.
  8.     //Needs to be set or object will fly into nowhere.
  9.     public Rigidbody2D target;
  10.     //Force multiplier. We multiply force with magnitude of 1(normalized) to this.
  11.     public float forcePower = 20f;
  12.     //Limit of speed. See its comment in FixedUpdate().
  13.     public float speedLimit = 5f;
  14.     void Start()
  15.     {
  16.         rb2D = GetComponent<Rigidbody2D>();
  17.         //Random speed at start.
  18.         rb2D.velocity = new Vector2(Random.Range(-speedLimit, speedLimit), Random.Range(-speedLimit, speedLimit));
  19.     }
  20.     //Measures distance in clockwise and counterclockwise directions between angles in radians and returns smallest
  21.     float ShortestAngleBetween(float anglestart, float anglefinish)
  22.     {
  23.         float counterClockwiseDistance, clockwiseDistance;//Counterclockwise
  24.         if (anglefinish < anglestart)
  25.         {
  26.             clockwiseDistance = anglestart - anglefinish;
  27.             counterClockwiseDistance = Mathf.PI * 2 - anglestart + anglefinish;
  28.         }
  29.         else {
  30.             clockwiseDistance = Mathf.PI * 2 - anglefinish + anglestart;
  31.             counterClockwiseDistance = anglefinish - anglestart;
  32.         }
  33.         if (counterClockwiseDistance < clockwiseDistance) return counterClockwiseDistance;
  34.         else return -clockwiseDistance;
  35.     }
  36.     //Checks angle in radians between vectors. Positive is counterClockwise, negative is clockwise.
  37.     float AngleBetweenVectors(Vector2 first, Vector2 second)
  38.     {
  39.         float ang1 = Mathf.Atan2(first.y, first.x), ang2 = Mathf.Atan2(second.y, second.x);
  40.         return ShortestAngleBetween(ang1, ang2);
  41.     }
  42.     void FixedUpdate()
  43.     {
  44.         Vector2 targetPosition;
  45.         //Target position with preemtion so that we can hit if it isn't stationary.
  46.         if (rb2D.velocity.magnitude > 0.0001f)
  47.             targetPosition = (target.position + target.velocity * (rb2D.position - target.position).magnitude / rb2D.velocity.magnitude);
  48.         else//Division by zero case
  49.             targetPosition = target.position;
  50.         //Direction we want to fly
  51.         Vector2 direction = targetPosition - rb2D.position;
  52.         //Angle difference coefficient 30 here can be modified. Smaller means we rotate to needed angle aggressively
  53.         float angleChangeMagnitude = AngleBetweenVectors(direction, rb2D.velocity) * Mathf.Rad2Deg / 30;
  54.         //Force basis.
  55.         Vector2 resultingForce = new Vector2(angleChangeMagnitude, 1);
  56.         //We take force basis, normalize it and multiply by power.
  57.         rb2D.AddRelativeForce(forcePower * resultingForce.normalized);
  58.         //Rotate the object itself to face where it flies. -90 because we fly to Y, not X.
  59.         rb2D.rotation = Mathf.Rad2Deg * Mathf.Atan2(rb2D.velocity.y, rb2D.velocity.x) - 90;
  60.         //Speed limit. Without it, we will often miss as missile will go spiral and, won't have enough side force to rotate properly.
  61.         //Can be solved by multiplying side force (angleChangeMagnitude) by value that depends on rb2D.velocity.magnitude.
  62.         if (rb2D.velocity.magnitude > speedLimit)
  63.             rb2D.velocity = rb2D.velocity.normalized * speedLimit;
  64.     }
  65.     //Note: for simplicity, collisions don't check if we hit another missile. That doesn't affect result much.
  66.     public void OnCollisionEnter2D(Collision2D collision)//In case we don't use trigger collider
  67.     {
  68.         Debug.Log("Hit");
  69.         Destroy(gameObject);
  70.     }
  71.     public void OnTriggerEnter2D(Collider2D collision)//In case we use trigger collider
  72.     {
  73.         Debug.Log("Hit");
  74.         Destroy(gameObject);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement