Unity移动物体

移动物体的方法有很多种,总结一下:

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值