遇到一个问题,敌人会向玩家射击,但即使玩家静止并且没有移动,也总是看起来很高或者在玩家一侧。我在代码中做错了什么造成了这个疯狂的问题,还是只是一个随机的烦人的错误?
为播放器使用相同的脚本,虽然它以不同的名称起作用,这让我相信问题在于火点。根据玩家的剧本,我会这样开火:
// 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;
}
图片供参考:
答案 0 :(得分:2)
计算速度的等式看起来令人怀疑。让我们重新推导它:
恒定重力下的自由落体运动方程是:
通过将第一个代入第二个来重新排列后,我们找到了一个表达式:
这与你所拥有的不同,因为你错过了h/d
这个词;所述术语还对θ
:
(基本上是指如果你直接在目标上发射 ,子弹将永远不会因重力而到达)
您的代码还有许多其他问题;仅举三个:
height
设为零?distance
添加更正?修正没有物理解释。修改后的代码:
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;
}
答案 1 :(得分:1)
我认为你错误地使用了Raycast。根据{{3}},第二个参数是方向,而不是目的地:
if (Physics.Raycast(player.transform.position,transform.position, out hitInfo))
应该是
if (Physics.Raycast(transform.position, player.transform.position -
transform.position, out hitInfo))
这可以解释为什么它没有在适当的时刻开火以及为什么方向不准确(因为hitInfo
是错误的)