c# - 2d 射击鼠标跟随问题。统一二维
问题描述
我正在制作 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 );
推荐阅读
- vba - 将函数的结果写入变量,其中结果可以是对象
- laravel-5 - 是否可以在一个系统 Laravel 上创建多个 swagger 文档页面?
- c - 如何在不同的文件中使用struct c编程
- ruby-on-rails - 如何在同一个表的两个实例之间有效地交换值
- c# - LINQ Fluent 语法 - 取消选择数组中的项目
- django - 如何在 django 中创建多个表进行身份验证
- javascript - 从其他模态打开模态,Bootstrap 3
- php - 遇到非数字值 - Laravel
- php - 如何取消设置克隆的 PHP 对象的 id
- scala - 如何将每个整数转换为 Scala 列表中的绝对值?