噩梦系列篇之相机平滑跟随Player及Tag的设置

player 的运动实现了那么相机就必须要跟随才可以。。。。那么当然用character controller是会省很多事情的。但是下面是自定义相机跟随的。。。


player是在原点的位置的。。。。通过观察发现相机在如下位置最适合看到场景和player。相机的位置如图:

噩梦系列篇之相机平滑跟随Player及Tag的设置_第1张图片


那么最适合的时候就该点击Align with view ,这样相机就会适应到你当前观察的位置。。。

噩梦系列篇之相机平滑跟随Player及Tag的设置_第2张图片


下面就来写脚本来实现相机的跟随;


using UnityEngine;
using System.Collections;

public class followtarget : MonoBehaviour {

    public float smooth=3f;//平滑跟随

 
    private Transform player;//定义player的位置

	void Start () {

    
        player = GameObject.FindGameObjectWithTag(Tag.player).transform;//获取player的位置

	}
	
	
	void Update () {

        Vector3 targetposition = player.position +new Vector3(0,6,-9);//目标位置点

        transform.position = Vector3.Lerp(transform.position,targetposition,smooth*Time.deltaTime);//从相机当时的位置跟随到目标的位置
	}
}

搜噶!!!只要把这个脚本绑定到相机上就OK了。。。。注意下面:

代码里看到的player = GameObject.FindGameObjectWithTag(Tag.player).transform;这个需要给player设定Tag,

噩梦系列篇之相机平滑跟随Player及Tag的设置_第3张图片

当然这个Tag加入的方法用如下代码(所有Tag添加方法都一样);

using UnityEngine;
using System.Collections;

public class Tag  {

    public const string player = "Player";
}




你可能感兴趣的:(噩梦系列游戏实例)