利用ITWEEN实现照相机的移动和转向

第一次写博客请多多指教呀~~

废话不多说,这次实现的功能是利用ITWEEN实现照相机的移动和转向。

首先搭建简单场景:两个Cube,正方体作为移动目标,长方体作为方向目标

通过以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class move : MonoBehaviour {

     public GameObject cube1;
     public GameObject cube2;

    void Start() {
        this.GetComponent<Button>().onClick.AddListener(delegate () {
            //    Debug.Log("点击了按钮事件一");
            iTween.MoveUpdate(Camera.main.gameObject, iTween.Hash(
            "x", cube1.transform.position.x,
            "y", cube1.transform.position.y,
            "z", cube1.transform.position.z,
            "time", 0.01,
            "islocal", true));

        });    
    }
    
    
    void FixedUpdate () {
            iTween.LookUpdate(transform.gameObject, cube2.transform.position, 1.0f);
    }
}
 

MoveTo使目标移动到指定的Vect3位置

LookTo使用起来和LookAt相似,可以让物体朝目标位置看去

把代码挂在照相机上(也试用所有其它被挂物体),运行就可以实现游戏场景里的Camera平滑运动转向,而不用通过切换照相机切换视角。运行过后Camera位置:

此时小伙伴会发现效果是先移动后转向,要实现移动转向同步,将转向的时间直接设为0f就好了

将时间设置为0后,运行后会发现镜头会发生震动

原因在LookTo,换成LookUpdate(不用依赖EasrType,与update中调用)
 

猜你喜欢

转载自blog.csdn.net/weixin_41814169/article/details/85275260