Fork me on GitHub

unity_实用小技巧(相机跟随两个主角移动)

在两人对战的游戏中,有时候我们希望能看清楚两玩家的状态,这时我们需要让相机跟随玩家,可是我们不能让相机只跟随一个玩家移动,这时我们可以取两玩家的中点作为相机的位置。方法如下:

 public Transform player1;
    public Transform player2;

    private Vector3 offset;
    private Camera camera;


    void Start()
    {
        offset = transform.position - ( player1.position + player2.position ) / 2; //偏移位置
        camera = this.GetComponent<Camera>();
    }

 
    void Update()
    {                      
        transform.position = ( player1.position + player2.position ) / 2 + offset; //设置相机位置
        float distance = Vector3.Distance(player1.position, player2.position);  //两玩家之间的距离
        float size = distance * 0.58f;//具体值根据游戏需要可自行调节
        camera.orthographicSize = size;
    }

其实相机跟随的方法都差不多,具体位置由偏移位置决定,因此确定偏移位置才是相机跟随最主要的区别

posted @ 2017-11-01 16:59  爱上游戏开发  阅读(1055)  评论(0编辑  收藏  举报
 >>>转载请注明出处