首页 > 解决方案 > 玩家旋转时子弹实例化角度错误

问题描述

我创建了一个具有 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);
    }    
}

标签: c#unity3dgame-development

解决方案


您正在使用硬编码的角度10345无论您看向哪个方向。

然后使用 eglaser02.right总是返回与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将直接返回相应的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);
    }
}

推荐阅读