您的位置:首页 > 移动开发 > Unity3D

Unity脚本,平滑跟随目标物体移动和旋转

2015-01-09 12:26 2221 查看
#pragma strict
var targetMoto:Transform; // 目标物体

var smoothPositionTime = 0.05;
private var xVelocitx = 0.0;
private var yVelocity = 0.0;
private var zVelocitz = 0.0;

var smoothAngleTime = 0.1;
private var xAnglex = 0.0;
private var yAngley = 0.0;

function Start () {
//	online = false;
}

function Update () {

//transform.position = targetMoto.position; // 位移
// 位移平滑阻尼
var newPositionx : float = Mathf.SmoothDamp(transform.position.x, targetMoto.position.x,xVelocitx, smoothPositionTime);
var newPositiony : float = Mathf.SmoothDamp(transform.position.y, targetMoto.position.y,yVelocity, smoothPositionTime);
var newPositionz : float = Mathf.SmoothDamp(transform.position.z, targetMoto.position.z,zVelocitz, smoothPositionTime);
transform.position = Vector3(newPositionx, newPositiony, newPositionz);

// 旋转平滑阻尼
var newAnglex : float = Mathf.SmoothDampAngle(transform.localEulerAngles.x,targetMoto.localEulerAngles.x, xAnglex, smoothAngleTime);
var newAngley : float = Mathf.SmoothDampAngle(transform.localEulerAngles.y,targetMoto.localEulerAngles.y, yAngley, smoothAngleTime);

transform.localEulerAngles.x = newAnglex;
transform.localEulerAngles.y = newAngley;
transform.localEulerAngles.z=0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: