【发布时间】:2020-03-25 19:38:26
【问题描述】:
围绕同一主题提出了很多问题,但似乎没有什么对我有用。
问题很简单,一个玩家和一个敌人在 x,y 平面上。我想以一个计算好的角度发射我的弹丸,这样弹丸就会在它的坐标处击中敌人。
我都试过了
Angle of Reach 和 Angle required to hit x,y
这两种实现最终都为我做同样的事情; Shooting but not hitting the target in this manner
任何帮助或指点将不胜感激!谢谢
代码如下:
public Rigidbody projectile;
public float projectileSpeed;
public float Firerate = 9f;
private float nextfire;
private GameObject enemy;
private float gravity = Physics.gravity.y;
private Vector3 directionalVector;
// Start is called before the first frame update
void Start()
{
enemy = GameObject.FindGameObjectWithTag("enemy");
}
void Update()
{
directionalVector = enemy.transform.position - transform.position;
}
void FixedUpdate()
{
nextfire = Time.time + (1 / Firerate);
float projectileSpeed2 = projectileSpeed * projectileSpeed;
float projectileSpeed4 = projectileSpeed2 * projectileSpeed2;
float x = enemy.transform.position.x;
float y = enemy.transform.position.y;
float x2 = x * x;
float theta = Mathf.Atan(projectileSpeed2-Mathf.Sqrt(projectileSpeed4-gravity*(gravity*x2+2*y*projectileSpeed2))/gravity*x);
print(theta);
Vector3 releaseVector = (Quaternion.AngleAxis(theta, Vector3.up) * directionalVector).normalized;
Debug.DrawRay(transform.position, releaseVector, Color.red,0.5f);
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = releaseVector * projectileSpeed;
}
}
【问题讨论】:
标签: unity3d projectile