如何让子弹预制件的精灵使用与我的武器相同的旋转?



我正在制作一个自顶向下的概念,其中枪支围绕玩家旋转,并根据你的准星所在的方向翻转(类似于ZERO Sievert)。我试图让我的子弹精灵有正确的旋转射击时,相对于我的球员的武器。

下面是我如何在射击脚本中实例化子弹,它以正确的方式发射,但精灵本身没有正确旋转。

void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firingPoint.position, firingPoint.rotation);      
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firingPoint.right * bulletForce, ForceMode2D.Impulse);  
}

在我的武器处理脚本中,这是我对武器旋转的实现,我现在正在翻转武器的y刻度来纠正精灵。


private void FixedUpdate()
{
RotateWeapon();
if (crosshair.transform.position.x < 0)
{
FlipWeapon();
}
}
void RotateWeapon()
{
float AngleRad = Mathf.Atan2(crosshair.transform.position.y - currentWeapon.transform.position.y, crosshair.transform.position.x - currentWeapon.transform.position.x);
float AngleDeg = (180 / Mathf.PI) * AngleRad;
currentWeapon.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
}
void FlipWeapon()
{
currentScale = transform.parent.localScale;
currentScale.y *= -1;
currentWeapon.transform.localScale = currentScale;
}

我目前正处于如何实现这一目标的停滞状态,因为我遇到的大多数自上而下射击资源都让玩家旋转到360度,而我的玩家只能向左或向右,武器本身在翻转之前只有180度的运动范围。

也许你可以尝试添加一个偏移量,这样当你实例化项目符号时,它会看起来像这样:

GameObject bullet = Instantiate(bulletPrefab, firingPoint.position, firingPoint.rotation + offset);

在这里offset将是一个旋转的x y z坐标,所以你会想要在z坐标上旋转它(尝试所有的,直到你找到正确的那个)并旋转它-90度或90度。你可能需要旋转它180度,但不要用270度,而是用-90度。相信我,这将对你日后的编码有所帮助。还要确保将偏移量设置为一个变量:

private Vector3 offset = 0, 0, -90;

!记住你可以改变x y z坐标!

最新更新