2d射击鼠标跟随问题。 Unity 2d

问题描述 投票:0回答:1

我正在制作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);


    }

任何帮助都已用尽。

c# unity3d
1个回答
0
投票

Mathf.Sign用于在计算旋转度时考虑x刻度的符号:

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(transform.localScale.x));
© www.soinside.com 2019 - 2024. All rights reserved.