简体   繁体   English

如何修复 Unity 中的子弹旋转? (2D)

[英]How to fix Bullet Rotation in Unity? (2D)

So i'm having issues with a script I have, what it does is basically rotate the player graphics depending on where the mouse is aiming at.所以我的脚本有问题,它的作用基本上是根据鼠标瞄准的位置旋转播放器图形。 This works fine and as intended but my issue is that when I want to shoot on the opposite direction, that being the Left direction, it shoots the player instead of where it's aiming at.这工作正常且符合预期,但我的问题是,当我想朝相反方向射击时,即向左方向射击时,它会射击玩家而不是瞄准的地方。

I've decided to record a small video to show the problem.我决定录制一个小视频来展示这个问题。

https://streamable.com/02zqz https://streamable.com/02zqz

Here's the code to both the rotation and weapon script.这是旋转和武器脚本的代码。

Rotating旋转

public class Rotating : MonoBehaviour
{
    public PlayerController player;
    public float x;

    public Vector3 flipArm;

    void Start()
    {
        x = transform.localScale.x;
        flipArm = transform.localScale;
        player = GetComponentInParent<PlayerController>();

    }

    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition ) - transform.position;
        difference.Normalize();

        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + 0);

        if(difference.x >= 0)
        {
            transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
            player.armRight = true;
        }
        else
        {
            transform.rotation = Quaternion.Euler(0f, 0f, rotZ+180);
            player.armRight = false;
        }
    }
}

Weapon武器

public class Weapon : MonoBehaviour
{
    public float shootDelay = 0;
    public float damage = 1;

    public LayerMask whatToHit;

    public Transform firePoint;

    public GameObject bullet;

    // Start is called before the first frame update
    void Start()
    {
        firePoint = transform.Find("FirePoint");
    }

    // Update is called once per frame
    void Update()
    {
        shootDelay += Time.deltaTime;
        if(shootDelay >= 0.1f)
        {
            if(Input.GetMouseButton(0))
            {
                shootDelay = 0;
                Shot();
            }
        }
    }

    public void Shot()
    {
        Vector2 mousepos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);

        Vector2 firepointpos = new Vector2(firePoint.position.x, firePoint.position.y);

        Instantiate(bullet, firepointpos, transform.rotation);

        Debug.DrawLine(firepointpos, (mousepos - firepointpos) * 100, Color.cyan);
    }
}

Try this:尝试这个:

transform.rotation = Quaternion.LookRotation ( end - start );

Also don't forget to check where the player is facing because you don't want to shoot from back.另外不要忘记检查玩家面对的位置,因为您不想从后面射击。 Using the above euler angles, one side is 90 and another 270 degrees in y component.使用上述欧拉角,y 分量的一侧为 90 度,另一侧为 270 度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM