2

当我想将相机从原点移动到目标位置时,它看起来很僵硬。所以如果它可以根据偏移量设置移动速度,怎么办?

4

4 回答 4

3

关于这个问题有一个很好的教程,通常在统一网站上所有教程的开头都有描述。生存射击教程中,有一个说明如何在移动时使相机平滑地移动到目标位置。

这是移动相机的代码。创建一个脚本,将其添加到相机,然后将GameObject要移动到的脚本添加到脚本占位符中。它将自动保存转换组件,如脚本中的设置。(在我的例子中,它是生存射击教程的玩家):

public class CameraFollow : MonoBehaviour
{
    // The position that that camera will be following.
    public Transform target; 
    // The speed with which the camera will be following.           
    public float smoothing = 5f;        

    // The initial offset from the target.
    Vector3 offset;                     

    void Start ()
    {
        // Calculate the initial offset.
        offset = transform.position - target.position;
    }

    void FixedUpdate ()
    {
        // Create a postion the camera is aiming for based on 
        // the offset from the target.
        Vector3 targetCamPos = target.position + offset;

        // Smoothly interpolate between the camera's current 
        // position and it's target position.
        transform.position = Vector3.Lerp (transform.position, 
                                           targetCamPos,   
                                           smoothing * Time.deltaTime);
    }
}
于 2015-07-23T09:27:55.633 回答
2

您可以使用iTween插件。如果您希望您的相机与您的对象一起移动,即顺利地跟随它。您可以使用smoothFollow脚本。

于 2014-01-03T08:06:18.407 回答
1
public Vector3 target;        //The player
public float smoothTime= 0.3f;  //Smooth Time
private Vector2 velocity;       //Velocity
Vector3 firstPosition;
Vector3 secondPosition;
Vector3 delta;
Vector3 ca;
tk2dCamera UICa;
bool click=false;
void Start(){
    ca=transform.position;
    UICa=GameObject.FindGameObjectWithTag("UI").GetComponent<tk2dCamera>();
}
void  FixedUpdate ()
{
    if(Input.GetMouseButtonDown(0)){


        firstPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
        ca=transform.position;

    }
    if(Input.GetMouseButton(0)){


        secondPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
        delta=secondPosition-firstPosition;
        target=ca+delta;

    }
    //Set the position
    if(delta!=Vector3.zero){
        transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, target.x, ref velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.y, ref velocity.y, smoothTime),transform.position.z);
    }

}
于 2014-01-13T07:56:42.560 回答
1

您是否尝试过使用 Vector3.MoveTowards?您可以指定要使用的步长,它应该足够平滑。

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html

于 2014-01-03T17:27:11.137 回答