Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- namespace Assets
- {
- public class AutopilotController : MonoBehaviour
- {
- [SerializeField] private Transform _target;
- [SerializeField] private Vector3 _initialVelocity;
- [SerializeField] private Single _acceleration;
- private void Start()
- {
- GetComponent<Rigidbody>().velocity = _initialVelocity;
- }
- private void Update()
- {
- var thisRigidbody = GetComponent<Rigidbody>();
- Vector2 tdir = _target.position - transform.position;
- Vector2 velocity = thisRigidbody.velocity;
- thisRigidbody.AddForce(CalculateForce(tdir, velocity));
- }
- private Vector2 CalculateForce(Vector2 tdir, Vector2 velocity)
- {
- return (Mathf.Sqrt(_acceleration * tdir.magnitude) * tdir.normalized - velocity)
- .normalized * _acceleration;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement