C# 如何逆时针旋转对象?
这个问题我找不到一个答案。我很抱歉,如果这是一个重复的职位,但我找不到任何东西C# 如何逆时针旋转对象?,c#,unity3d,C#,Unity3d,这个问题我找不到一个答案。我很抱歉,如果这是一个重复的职位,但我找不到任何东西 我试图在Unity中的2D游戏中逆时针设置一个对象的动画。我假设必须有一个简单的一行或两行代码来实现这一点,但我无法理解。我读到的所有东西都说顺时针旋转很容易,但单位使逆时针旋转更复杂?在我们进入这个问题的编码位之前。 你首先需要理解你想要实现的目标背后的逻辑。 逆时针方向正好是要旋转的角度,但被否定(-) 要在二维中围绕对象旋转,只需执行以下操作: using UnityEngine; public class
我试图在Unity中的2D游戏中逆时针设置一个对象的动画。我假设必须有一个简单的一行或两行代码来实现这一点,但我无法理解。我读到的所有东西都说顺时针旋转很容易,但单位使逆时针旋转更复杂?在我们进入这个问题的编码位之前。 你首先需要理解你想要实现的目标背后的逻辑。 逆时针方向正好是要旋转的角度,但被否定(-) 要在二维中围绕对象旋转,只需执行以下操作:
using UnityEngine;
public class Example : MonoBehaviour
{
void Update()
{
// Spin the object around the world origin at 20 degrees/second.
transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
}
}
在2D中,旋转轴将变为Vector3.back或Vector3.forward,具体取决于要旋转的方向
示例来自:
正如用户“程序员”所指出的:transform.Rotate
如果您不想让对象围绕另一个对象旋转,则可以使用该技巧
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Rotate the object around its local X axis at 1 degree per second
transform.Rotate(Vector3.right * Time.deltaTime);
// ...also rotate around the World's Y axis
transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
}
}
示例来自:
因为OP进一步明确了他想要实现的目标。我将留下这些图像供参考:
话虽如此,但请尝试:
Vector3 direction = player.transform.position - iss.transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * mathf.Rad2Deg;
iss.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
如果使用的是
transform.Rotate()
函数,则只需对最后一个参数求反即可。
默认旋转是顺时针的,因此,如果将顺时针移动的值取反,则将使旋转为逆时针
例如:
public float rotationSpeed = 100f;
public void rotateClockwise()
{
transform.Rotate(0f, 0f, (rotationSpeed * Time.deltaTime));
}
public void rotateCtrClockwise()
{
transfrom.Rotate(0f, 0f, -(rotationSpeed * Time.deltaTime));
}
如果你可以顺时针旋转,那么只需取消逆时针旋转的角度。OP想要旋转一个对象而不是围绕另一个对象旋转对象…
transform.rotate
在这里比transform.RotateAround
更合适。很抱歉,我在我的原始帖子中没有澄清这一点,但我确实知道transform.rotate(Vector3.right*Time.deltaTime),但正如你所说,它是绕x轴旋转的,或者y轴向上旋转。我的问题是,因为它是一个2D程序,所以我需要沿Z轴旋转它。我该怎么做才能在Z轴上逆时针旋转它?我留下了两张图片,以供参考,说明Z轴如何影响2D中的场景,以及图片下方的解决方案。很抱歉,我已经有一段时间没有联机了,我标记了ed解决了,谢谢你的帮助。