unity3d 逐帧旋转和平移 Vector3.MoveTowards和Vector3.RotateTowards
Vector3.RotateTowards
public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);
Parameters
current | The vector being managed. |
---|---|
target | The vector. |
maxRadiansDelta | The distance between the two vectors in radians. |
maxMagnitudeDelta | The length of the radian. |
Returns
Vector3 The location that RotateTowards generates.
Description
Rotates a vector current
towards target
.
This function is similar to MoveTowards except that the vector is treated as a direction rather than a position. The current
vector will be rotated round toward the target
direction by an angle of maxRadiansDelta
, although it will land exactly on the target rather than overshoot. If the magnitudes of current
and target
are different, then the magnitude of the result will be linearly interpolated during the rotation. If a negative value is used for maxRadiansDelta
, the vector will rotate away from target/
until it is pointing in exactly the opposite direction, then stops.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
// The target marker.
Transform target;
// Angular speed in radians per sec.
float speed;
void Update()
{
Vector3 targetDir = target.position - transform.position;
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.red);
// Move our position a step closer to the target.
transform.rotation = Quaternion.LookRotation(newDir);
}
}
Vector3.MoveTowards
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
Parameters
current | The position to move from. |
---|---|
target | The position to move towards. |
maxDistanceDelta | Distance to move current per call. |
Returns
Vector3 The new position.
Description
Calculate a position between the points specified by current
and target
, moving no farther than the distance specified by maxDistanceDelta
.
Use the MoveTowards member to move an object at the current
position toward the target
position. By updating an object’s position each frame using the position calculated by this function, you can move it towards the target smoothly. Control the speed of movement with the maxDistanceDelta
parameter. If the current
position is already closer to the target
than maxDistanceDelta, the value returned is equal to target
; the new position does not overshoot target
. To make sure that object speed is independent of frame rate, multiply the maxDistanceDelta
value by Time.deltaTime (or Time.fixedDeltaTime in a FixedUpdate loop).
Note that if you set maxDistanceDelta to a negative value, this function returns a position in the opposite direction from the 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;
}
}
}