Player的跟踪狂 -- Camera

P.S.很多游戏里的Player都会设置的被跟踪,是人性的扭曲,还是XXX,正在解密。

第三人称视角

  1. camera紧跟player背后(角度随player改变)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour {

    private Transform player;
    private Vector3 offset;//偏移值
    private float smoothing = 3.0f;//平滑度

	// Use this for initialization
	void Start () {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        offset = transform.position - player.position;
	}
	
	// Update is called once per frame
	void LateUpdate () {
        Vector3 targetPosition = player.position + player.TransformDirection(offset);//局部坐标,跟随背后
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
        transform.LookAt(player);
	}
}

效果如下:(一直保持这个视角)
image

  1. camera保持一个角度做俯视(像被上帝注视,颤抖吧player!)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour {

    private Transform player;
    private Vector3 offset;
    private float smoothing = 3.0f;//平滑度

	// Use this for initialization
	void Start () {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        offset = transform.position - player.position;
	}
	
	// Update is called once per frame
	void LateUpdate () {
        Vector3 targetPosition = player.position + offset;//世界坐标,上帝视角
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
        transform.LookAt(player);
	}
}

效果如下:(可以看到身前)
image

两个跟踪方式只有一行代码的不同,但是游戏体验效果差很多,自己实现体现被跟踪的乐趣吧。
posted @ 2018-10-28 21:44  JoharWong  阅读(174)  评论(0编辑  收藏  举报