我正在制作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刻度的符号:
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));