首页 > 解决方案 > 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)使对象快速向右移动,并以中间位置为中心,然后从右侧开始移动,我无法控制哪个角度它将开始。

标签: c#visual-studiounity3d

解决方案


尝试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);   
    }
}

检查文档。你设置旋转点、旋转轴和角度,就是这样。


推荐阅读