UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Vector3.MoveTowards
# [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html).MoveTowards public static [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **MoveTowards** ([Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **current**, [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **target**, float **maxDistanceDelta**); ## 参数 | current | 移动的开始位置。 | | ------------------ | ------------------------------------- | | target | 移动的目标位置。 | | maxDistanceDelta | 每次调用移动 `current` 的距离。 | ## 返回 **Vector3** 新位置。 ## 描述 计算 `current` 指定的点与 `target` 指定的点之间的位置,移动距离不超过 `maxDistanceDelta` 指定的距离。 使用 [MoveTowards](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.MoveTowards.html) 成员将 `current` 位置处的对象移向 `target` 位置。通过使用此函数计算的位置来更新每个帧的对象位置,您可以平滑地将其移向目标。使用 `maxDistanceDelta` 参数来控制移动的速度。如果 `current` 位置相比 [maxDistanceDelta](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-maxDistanceDelta.html) 更靠近 `maxDistanceDelta`,则返回值等于 `maxDistanceDelta`;新位置不会过冲 `maxDistanceDelta`。要确保对象速度与帧率无关,请将 `maxDistanceDelta` 值乘以 [Time.deltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-deltaTime.html)(或者 FixedUpdate 循环中的 [Time.fixedDeltaTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/Time-fixedDeltaTime.html))。 请注意,如果将 [maxDistanceDelta](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-maxDistanceDelta.html) 设置为负值,则该函数将返回与 `target` 相反方向的位置。 ``` using UnityEngine; using System.Collections; // Vector3.MoveTowards example. // A cube can be moved around the world. It is kept inside a 1 unit by 1 unit // xz space. A small, long cylinder is created and positioned away from the center of // the 1x1 unit. The cylinder is moved between two locations. Each time the cylinder is // positioned the cube moves towards it. When the cube reaches the cylinder the cylinder // is re-positioned to the other location. The cube then changes direction and moves // towards the cylinder again. // // A floor object is created for you. // // To view this example, create a new 3d Project and create a Cube placed at // the origin. Create Example.cs and change the script code to that shown below. // Save the script and add to the Cube. // // Now run the example. public class Example : MonoBehaviour { // Adjust the speed for the application. public float speed = 1.0f; // The target (cylinder) position. private Transform target; void Awake() { // Position the cube at the origin. transform.position = new Vector3(0.0f, 0.0f, 0.0f); // Create and position the cylinder. Reduce the size. GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); Camera.main.transform.position = new Vector3(0.85f, 1.0f, -3.0f); // Grab cylinder values and place on the target. target = cylinder.transform; target.transform.localScale = new Vector3(0.15f, 1.0f, 0.15f); target.transform.position = new Vector3(0.8f, 0.0f, 0.8f); // Position the camera. Camera.main.transform.position = new Vector3(0.85f, 1.0f, -3.0f); Camera.main.transform.localEulerAngles = new Vector3(15.0f, -20.0f, -0.5f); // Create and position the floor. GameObject floor = GameObject.CreatePrimitive(PrimitiveType.Plane); floor.transform.position = new Vector3(0.0f, -1.0f, 0.0f); } void Update() { // Move our position a step closer to the target. float step = speed * Time.deltaTime; // calculate distance to move transform.position = Vector3.MoveTowards(transform.position, target.position, step); // Check if the position of the cube and sphere are approximately equal. if (Vector3.Distance(transform.position, target.position) < 0.001f) { // Swap the position of the cylinder. target.position *= -1.0f; } } } ```
da
2022年6月22日 10:56
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码