Advertisement
ArXen42

StupidAutopilot2.cs

Sep 24th, 2016
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace Assets
  5. {
  6.     public class AutopilotController : MonoBehaviour
  7.     {
  8.         [SerializeField] private Transform _target;
  9.         [SerializeField] private Vector3 _initialVelocity;
  10.         [SerializeField] private Single _acceleration;
  11.  
  12.         private void Start()
  13.         {
  14.             GetComponent<Rigidbody>().velocity = _initialVelocity;
  15.         }
  16.  
  17.         private void Update()
  18.         {
  19.             var thisRigidbody = GetComponent<Rigidbody>();
  20.  
  21.             Vector2 tdir = _target.position - transform.position;
  22.             Vector2 velocity = thisRigidbody.velocity;
  23.  
  24.             thisRigidbody.AddForce(CalculateForce(tdir, velocity));
  25.         }
  26.  
  27.         private Vector2 CalculateForce(Vector2 tdir, Vector2 velocity)
  28.         {
  29.             return (Mathf.Sqrt(_acceleration * tdir.magnitude) * tdir.normalized - velocity)
  30.                        .normalized * _acceleration;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement