close

在3D游戏中经常要用到摄像机跟着任务(target)运动

下面列出几个主要用到的方法

1.transform.eulerAngles.y  transform的y轴的欧拉角。此值代表以y为轴左右旋转的角度。用以得到在x-z平面的转动角度

2.Mathf.SmoothDampAngle (current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

 返回值是一个角度值。此方法的用于缓慢的转动角度,从current角度转到target 角度,用时为smoothTime,最大的转动角速度为maxSpeed。currentVelocity值的意义本人暂时还不太明白,文档中说这个值每 次在方法调用的时候都会改变。

本人认为,由于此方法一般用在update()中,也就是说,不是一次就完成的,角度的转换 可能需要通过很多次的调用update(),因此,current应该每次都变动,也就是此方法的返回值为转动后的当前的角度。这样,调用 update()最终能够实现从原始的current角度转到target角度。

3.Mathf.SmoothDamp(current : float, target : float, ref currentVelocity : float, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

在2中的方法主要用于角度值的转动,本方法则用于普通值(比如高度、距离)的转动。

在镜头转动的时候不能一下子就转过 去,否则会是玩家感觉很突然。用此方法是的距离慢慢转动。跟2的用法一样,在每次调用的时候不一定就能完成从current到target的转动,因此在 update()中,current应该是每次都变化。本方法的返回值恰好就是转动后的当前距离值。

可以这样写:

  current=Mathf.SmoothDamp(current,target,currentVelocity,smoothTime);

4.Quaternion.Euler (x : float, y : float, z : float) : Quaternion

四元组的运用在unity3d中用的很多。特别是在计算角度值的时候。

方法的意思是:沿着x轴转动x度,沿着y轴转动y度,沿着z轴转动z度。

var currentRotation = Quaternion.Euler (0, Angle, 0);

currentRotation * Vector3.back * distance;

这段代码的意思是使得Vector3.back向量沿着y轴旋转Angle度。向量的长度是distance。

Vector3.back=(0,0,-1)的方向向量

 

arrow
arrow
    全站熱搜

    GABY 發表在 痞客邦 留言(0) 人氣()