遇到了一个问题,即敌人会向玩家开火,但似乎总是开到高处或向玩家的一侧,即使玩家是静止的并且没有移动。我是不是在我的代码中做错了什么导致了这个疯狂的问题,或者它只是一个随机的烦人的错误?
为播放器使用相同的脚本,尽管它的名称不同,但我认为问题出在火点内。在玩家的脚本下我是这样开火的:
// Get the place the player has clicked
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Holds information regarding the mouseclick
RaycastHit hitInfo;
// Now work out if we fire or not
if (Physics.Raycast(ray, out hitInfo))
{
if(hitInfo.distance < maxRange)
{
FireAtPoint(hitInfo.point);
而在敌人脚本中,它只是通过玩家的位置完成的。
// Holds information regarding the mouseclick
RaycastHit hitInfo;
// Now work out if we fire or not
if (Physics.Raycast(player.transform.position,transform.forward, out hitInfo))
{
那么这是 Physics.Raycast 调用中的潜在问题吗?
其余代码供引用:
//More above this but doesn't influence the firing
if (Physics.Raycast(player.transform.position,transform.position, out hitInfo))
{
if (hitInfo.distance < maxRange)
{
FireAtPoint(hitInfo.point);
}
}
private void FireAtPoint(Vector3 point)
{
// Get the velocity to fire out at
var velocity = FiringVelocity(point, angle);
Rigidbody rg = Instantiate(bulletPrefab.gameObject, firePoint.position, firePoint.rotation).GetComponent<Rigidbody>();
EnemyBulletController newProjectile = rg.GetComponent<EnemyBulletController>();
newProjectile.speed = velocity;
}
private Vector3 FiringVelocity(Vector3 destination, float angle)
{
// Get the direction of the mouse click from the player, then get the height differential.
Vector3 direction = destination - transform.position;
float height = direction.y;
height = 0;
// Get the distance in a float of the vector3
float distance = direction.magnitude;
// Turn the firing angle into radians for calculations, then work out any height differential
float AngleRadians = angle * Mathf.Deg2Rad;
direction.y = distance * Mathf.Tan(AngleRadians);
distance += height / Mathf.Tan(AngleRadians);
// Calculate the velocity magnitude
float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude / Mathf.Sin(2 * AngleRadians));
// Return the normalized vector to fire at.
return velocity * direction.normalized;
}
引用图片:
最佳答案
您计算速度的方程式看起来很可疑。让我们重新推导它:
恒重力下的自由落体运动方程为:
通过将第一个替换为第二个来重新排列后,我们找到射击速度的表达式:
这与您所拥有的不同,因为您缺少 h/d
术语;所述术语还对 θ
的允许值进行了限制:
(基本上意味着如果你直接向目标射击,子弹将永远不会由于重力而到达)
您的代码还有许多其他问题;仅列出三个:
- 为什么要将
height
设置为零? - 为什么要对
distance
进行修正?校正没有物理解释。 - @BasillePerrnoud 建议的修复
修改后的代码:
private Vector3 FiringVelocity(Vector3 destination, float angle)
{
Vector3 direction = destination - transform.position;
float height = direction.y;
float distance = Mathf.Sqrt(direction.x * direction.x + direction.z * direction.z); // *horizontal* distance
float radians = angle * Mathf.Deg2Rad;
float hOverd = height / distance;
float tanAngle = Mathf.Tan(radians);
if (tanAngle <= hOverd)
// throw an exception or return an error code, because no solution exists for v
float cosAngle = Mathf.Cos(radians);
direction.Y = distance / cosAngle;
float velocity = Mathf.Sqrt((distance * Physics.gravity.magnitude) /
(2 * cosAngle * cosAngle * (tanAngle - hOverd)));
return velocity * direction.normalized;
}
关于c# - Unity 敌人 ai 总是向玩家上方发射子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48791192/