移动物体的方法有很多种,总结一下:
1. 给其刚体添加一个力(AddForce-rigidbody必须)
transform.rigidbody.AddForce(transform.forward * shootSpeed);
2. 为其刚体设置速度(velocity-rigidbody必须)
atransform.rigidbody.velocity=btransform.TransformDirection(Vector3.forward*shootSpeed);
3. Translate 函数
function Update ()
{
transform.Translate(Vector3(1,0,0) * Time.deltaTime);
}
4. Vector3.Lerp
// Animates the position to move from start to end within one second
//在1秒时间动画位置移动从from开始到to结束。
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
// Follows the target position like with a spring
//像弹簧一样跟随目标物体
var target : Transform;
var smooth = 5.0;
function Update () {
transform.position = Vector3.Lerp (
transform.position, target.position,
Time.deltaTime * smooth);
}
5. Vector3.MoveTowards
Moves a point current in a straight line towards a target point.
The value returned by this function is a point maxDistanceDelta units closer to a target/ point along a line between current and target. If the target is closer than maxDistanceDelta/ then the returned value will be equal to target (ie, the movement will not overshoot the target). Negative values of maxDistanceDelta can be used to push the point away from the target.
// The target marker.
var target: Transform;
// Speed in units per sec.
var speed: float;
function Update () {
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
6. Vector3.SmoothDamp 平滑阻尼
平滑阻尼将比Lerp或MoveTowards更平滑
// Smooth towards the target
var target : Transform;
var smoothTime = 0.3;
private var velocity = Vector3.zero;
function Update () {
// Define a target position above and behind the target transform
var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 5, -10));
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition,
velocity, smoothTime);
}