Как плавно перемещать камеру в unity3D?

Когда я хочу переместить камеру из исходного положения в конечное положение, она выглядит очень жесткой. Так что, если она может установить скорость перемещения в соответствии со смещением, как это сделать?

4 ответа

Есть хороший учебник по этой проблеме, который обычно описывается в начале всех учебников на веб-сайте Unity. В руководстве Survival Shooter есть объяснение, как сделать движение камеры в конечной позиции плавным при движении.

Вот код для перемещения камеры. Создайте сценарий, добавьте его в камеру и добавьте GameObject Вы хотите перейти в заполнитель сценариев. Он автоматически сохранит компонент Transform, такой как настройка, в скрипте. (В моем случае это игрок учебника по стрельбе на выживание):

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);
    }
}

Вы можете использовать плагины iTween. И если вы хотите, чтобы ваша камера двигалась вместе с вашим объектом, то есть плавно следовала за ним. Вы можете использовать скрипт smoothFollow.

Вы пытались использовать Vector3.MoveTowards? Вы можете указать размер шага, который должен быть достаточно плавным.

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

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);
    }

}
Другие вопросы по тегам