c# - 当我点击播放时,我的 Sprite 角色会旋转。为什么?
问题描述
我正在 Unity 上制作一个自上而下的游戏,所以我使用 x 和 z 轴作为我的平面。我让我的角色旋转 x 90, y 0, z 0 使其平放在平面上。一旦我点击播放,角色就会垂直旋转?!我认为这与我的脚本面对鼠标位置有关。
它应该是什么样子:
当我点击播放时:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public static float moveSpeed = 10f;
private Rigidbody rb;
private Vector3 moveInput;
private Vector3 moveVelocity;
// Update is called once per frame
void Start()
{
rb = GetComponent<Rigidbody>();
mainCamera = FindObjectOfType<Camera>();
}
void Update()
{
// Setting up movement along x and z axis. (Top Down Shooter)
moveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveVelocity = moveInput * moveSpeed;
//Make character look at mouse.
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
}
void FixedUpdate()
{
// Allows character to move.
rb.velocity = moveVelocity;
}
}
解决方案
想通了:我正在回答自己的问题以帮助他人。
Vector3 difference = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(90f, 0f, rotZ -90);
这正是我想要的!
推荐阅读
- ruby-on-rails - 如何从一个范围订购前 10 个字段并将其与另一个范围连接?
- .htaccess - RewriteRule ^ index.php [L] 影响 301 重定向
- xml - 通过 XSLT 创建前导空格和空行的 XML 到文本文件
- macros - 让 @:genericbuild 工作的一些错误(每晚 haxe)
- rust - 在 x86_64-pc-windows-gnu 上编译 curl-sys 失败,未定义对 '__imp___acrt_iob_func' 的引用
- python - 如何将 beautifulsoup 的输出附加到 pandas 数据框
- reactjs - 替换和推送仍然无法在同一页面上工作
- scala - scala:使用常量键创建一个元组序列
- rollupjs - rollup-js 与 rollup-plugin-multi-entry - 多个文件输出/具有重复常量名称的问题
- c# - 如何使用分区以使用 .NET Core C# 并行使用 kafka 中的一个主题?