c# - Unity3D如何使对象开始使用半径在弧或圆中移动?
问题描述
Unity3D,C#
我想知道如何使用半径平滑移动对象,我希望对象从 transform.position 开始在一个圆圈中移动,如下所示:我想要做的示例,并且只使用 C# 脚本,我能做什么想到正在使用下面的变换。
timeCounter += Time.deltaTime * speed;
float x = Mathf.Cos(timeCounter) * width;
float z = Mathf.Sin(timeCounter) * height;
transform.position = new Vector3(x, 0, z);
我试过这个https://forum.unity.com/threads/how-to-make-an-object-move-in-a-circular-motion-at-constant-speed.526107/但它不是我的想要,我的答案(https://youtu.be/V2A-0yOUNwc)使对象快速向右移动,并以中间位置为中心,然后从右侧开始移动,我无法控制哪个角度它将开始。
解决方案
尝试Transform.RotateAround
:
using UnityEngine;
public class RotateAround : MonoBehaviour
{
public Vector3 rotatingPoint;
private Vector3 rotAxis = new Vector3(0,1,0);
void Start()
{
}
void Update()
{
transform.RotateAround(rotatingPoint, rotAxis, 1f);
}
}
检查文档。你设置旋转点、旋转轴和角度,就是这样。
推荐阅读
- python - 当我从批处理文件运行 .py 文件时打开 PyCharm
- python - 如何在 Redhat RHEL 7 离线机器上使用所有工具安装 python3
- asp.net-mvc - 如何在 ASP.NET Core 中每页显示一个模型项?
- php - Laravel - 未定义的变量:讲师
- python - 尝试解析 Word 文档并获取 PdfReadError: EOF marker not found
- c++ - Qt-5.14.0:QML 下的 Vulkan 导致 std::system_error:: 互斥锁失败
- uwp - 在哪里可以找到 UWP 控件的默认控件模板?
- android - 如何使用 NetworkBoundResource 在 MVVM 中使用网络错误响应更新 UI
- java - 如何使用正则表达式拆分 MCQ(多项选择题)字符串
- javascript - 触发 onclick 事件后保持引导程序打开