Unity3dフレームごとの回転と平行移動Vector3.MoveTowardsとVector3.RotateTowards



Unity3d Frame Frame Rotation



パブリックスタティック Vector3 向かって移動 (( Vector3 電流Vector3 目標 、 浮く maxDistanceDelta )。

パラメーター

電流 移動元の位置。
目標 に向かって移動する位置。
maxDistanceDelta 移動距離current呼び出しごと。

戻り値

Vector3 新しい位置。



説明

currentで指定されたポイント間の位置を計算しますおよびtargetmaxDistanceDeltaで指定された距離を超えないように移動します。

使用 向かって移動 currentでオブジェクトを移動するメンバーtargetに向かう位置ポジション。この関数で計算された位置を使用して、フレームごとにオブジェクトの位置を更新することで、オブジェクトをターゲットに向かってスムーズに移動できます。 maxDistanceDeltaで移動速度を制御しますパラメータ。 currentの場合位置はすでにtargetに近づいていますより maxDistanceDelta 、返される値はtargetに等しい新しい位置はオーバーシュートしませんtarget。オブジェクトの速度がフレームレートに依存しないようにするには、maxDistanceDeltaを乗算します。による値 Time.deltaTime (または Time.fixedDeltaTime FixedUpdateループ内)。



設定した場合は注意してください maxDistanceDelta 負の値にすると、この関数は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 } } }