玩家旋转时子弹实例化角度错误

Bullet instantiation angle is wrong when the player rotates

我创建了一个具有 3 个不同实例化位置的射弹子弹效果。当玩家面向右侧时,它可以完美运行。但是,当面向左侧时,子弹实例化的角度会误入歧途。任何帮助表示赞赏。

玩家右转时的截图

玩家左转时的截图。

播放器代码。

private IEnumerator FireContinuously()
{
    while (true)
    {
        GameObject laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10)));
        laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;
        GameObject laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Euler(new Vector3(0, 0, 0)));
        laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;
        GameObject laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345)));
        laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;
        yield return new WaitForSeconds(projectileFiringPeriod);
    }
}

public void Flipsprite()
{    
    bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > 0;

    if (playerhashorizontalspeed)
    {        
        direction = Mathf.Sign(myRigidBody.velocity.x);
        transform.localScale = new Vector3(direction, 1f);
    }    
}

无论您朝哪个方向看,您都在使用硬编码角度 10345

然后使用例如laser02.right 总是 return 相同 Vector3 只是你否定了它。这导致方向不正确。如果你比较图像,它就是你得到的方向。

您更想要的是也否定您旋转子弹的角度。

private IEnumerator FireContinuously()
{
    while (true)
    {
        var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
        laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;

        var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
        laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;

        var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
        laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;

        yield return new WaitForSeconds(projectileFiringPeriod);
    }
}

还有一点提示:

如果您想制作类型的预制件

[SerializeField] private RigidBody2D bullet01;
[SerializeField] private RigidBody2D bullet02;
[SerializeField] private RigidBody2D bullet03;

然后 Instantiate 将直接 return 根据 RigidBody2D 参考,您可以摆脱 GetComponent 调用:

private IEnumerator FireContinuously()
{
    while (true)
    {
        var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
        laser02.velocity = laser02.transform.right * projectileSpeed * direction;

        var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
        laser03.velocity = laser03.transform.right * projectileSpeed * direction;

        var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
        laser04.velocity = laser04.transform.right * projectileSpeed * direction;

        yield return new WaitForSeconds(projectileFiringPeriod);
    }
}