C# 2d shooter鼠标跟踪问题。统一2d

C# 2d shooter鼠标跟踪问题。统一2d,c#,unity3d,C#,Unity3d,我正在制作2d射击游戏,但我遇到了一个问题。当我的角色向左翻转其比例为-1时,武器保持的旋转将反转远离光标。这是我的鼠标跟随代码,以防需要 using System.Collections; using System.Collections.Generic; using UnityEngine; public float offset; void Update() { Vector3 difference = Camera.main.Scree

我正在制作2d射击游戏,但我遇到了一个问题。当我的角色向左翻转其比例为-1时,武器保持的旋转将反转远离光标。这是我的鼠标跟随代码,以防需要

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



    public float offset;



    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);


    }

任何帮助都是完全有用的

计算旋转时,使用
Mathf.Sign
将x刻度的符号考虑在内。翻转比例时,将使用它来抵消旋转的x分量和旋转角度本身:

Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) 
        - transform.position;
float scaleSign = Mathf.Sign(transform.localScale.x);
float rotZ = Mathf.Atan2(difference.y, difference.x * scaleSign) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, (rotZ + offset) * scaleSign );

将此添加到您的代码中

        if (rotZ < -90f || rotZ > 90f)
        {
            if (playerGameObject.transform.eulerAngles.y == 0f)
            {
                transform.localRotation = Quaternion.Euler (180f, 0f, rotZ); 
            }
            else if (playerGameObject.transform.eulerAngles.y == 180f)
            {
                transform.localRotation = Quaternion.Euler (180f, 180f, -rotZ); 
            }
        }
if(rotZ<-90f | | rotZ>90f)
{
if(playerGameObject.transform.eulerAngles.y==0f)
{
transform.localRotation=Quaternion.Euler(180f,0f,rotZ);
}
else if(playerGameObject.transform.eulerAngles.y==180f)
{
transform.localRotation=Quaternion.Euler(180f,180f,-rotZ);
}
}
整个事情会是这样的

    public float offset;

    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

        if (rotZ < -90f || rotZ > 90f)
        {
            if (playerGameObject.transform.eulerAngles.y == 0f)
            {
                transform.localRotation = Quaternion.Euler (180f, 0f, rotZ); 
            }
            else if (playerGameObject.transform.eulerAngles.y == 180f)
            {
                transform.localRotation = Quaternion.Euler (180f, 180f, -rotZ); 
            }
        }
    }
公共浮动偏移量;
无效更新()
{
Vector3差异=Camera.main.ScreenToWorldPoint(Input.mousePosition)-transform.position;
浮点数rotZ=Mathf.Atan2(差分y、差分x)*Mathf.Rad2Deg;
transform.rotation=Quaternion.Euler(0f,0f,rotZ+offset);
如果(rotZ<-90f | | rotZ>90f)
{
if(playerGameObject.transform.eulerAngles.y==0f)
{
transform.localRotation=Quaternion.Euler(180f,0f,rotZ);
}
else if(playerGameObject.transform.eulerAngles.y==180f)
{
transform.localRotation=Quaternion.Euler(180f,180f,-rotZ);
}
}
}

如果玩家被翻转,此代码会改变你旋转枪的方式

抱歉,但我认为我可能做错了什么,因为它仍然指向远离光标的方向。“我还有什么东西没找到吗?”布鲁奇金,我想我现在找到了。请尝试此最新版本。请包括如何翻转秤。我假设你只翻转x组件,但这只是一个假设。我在翻转时翻转它。当翻转时,XIt仍然是反转的。你看了吗:我真的建议不要使用“翻转x比例”方法,因为它确实会弄乱游戏对象的一些功能,如方向、碰撞器、,等等。如果你想让精灵看起来不同的方向,试着只翻转精灵/图像组件,而不是整个游戏对象本身:
playersprite.flipX=true
类似的东西:)