Unity中抛物线实现精准打击
unity中抛物线实现精准打击
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletControl : MonoBehaviour {
public Transform _Target;
public float _VerticalSpeed;
const float _Grivaty = -9.81f;
float _HorizontalSpeed;
float _TimeCount;
Vector3 _HorizontalDir;
void Start () {
float t1 = -_VerticalSpeed / _Grivaty;
//float s1 = -_Grivaty * t1 * t1 / 2;
float s1 = _VerticalSpeed * _VerticalSpeed / (2 * -_Grivaty);
float top = transform.position.y + s1;
float s2 = top - _Target.position.y;
_HorizontalDir = _Target.position - transform.position;
_HorizontalDir.y = 0;
float sh = Vector3.Magnitude(_HorizontalDir);
float t2 = Mathf.Sqrt(2 * s2 / -_Grivaty);
_HorizontalSpeed = sh / (t1 + t2);
}
void Update ()
{
_TimeCount += Time.deltaTime;
transform.Translate(_HorizontalDir.normalized * _HorizontalSpeed * Time.deltaTime);
transform.Translate(Vector3.up * (_VerticalSpeed + _Grivaty * _TimeCount) * Time.deltaTime);
Debug.DrawLine(transform.position, transform.position+Vector3.up, Color.red, 1000);
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!