基于Unity3D的相机功能的实现(六)—— 上帝视角(王者荣耀视角)

在MOBA游戏中,上帝视角是一个很实用的功能。

代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

//上帝视角(王者荣耀视角)
public class OverlookCamera : MonoBehaviour
{
	//Camera 水平移动速度
	public float speedH = 20f;
	//Camera 垂直移动速度
	public float speedV = 20f;
	//x方向最小值
	public float minX = -67f;
	//x方向最大值
	public float maxX = 70f;
	//y方向最小值
	public float minY = -75f;
	//y方向最大值
	public float maxY = 50f;
	//插值速度
	public float lerpSpeed=4f;

	//位置插值目标 
	private Vector3 posTarget;
	//屏幕的宽度
	private float screenWidth;
	//屏幕的高度
	private float screenHeight;

	void Start(){
		screenWidth = Screen.width;
		screenHeight = Screen.height;
		posTarget = transform.position;
	}
	void Update()
	{
		if (Input.touchCount == 1) {
			Vector2 touchPos = Input.GetTouch (0).position;
			if (touchPos.x >= screenWidth - 40 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
				if (transform.position.x <= maxX)
					posTarget = posTarget + Vector3.right * speedH * 0.02f;
			} else if (touchPos.x <= 40 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
				if (transform.position.x >= minX)
					posTarget = posTarget + Vector3.left * speedH * 0.02f;
			} else if (touchPos.y > screenHeight - 40 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
				if (transform.position.z <= maxY)
					posTarget = posTarget + Vector3.forward * speedV * 0.02f;
			} else if (touchPos.y < 40 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
				if (transform.position.z >= minY)
					posTarget = posTarget + Vector3.back * speedV * 0.02f;
			} else {
				float x = Input.GetAxis ("Mouse X");
				float y = Input.GetAxis ("Mouse Y");
				//x方向在最大值和最小值
				if (transform.position.x >= minX && transform.position.x <= maxX) {
					if (x < -0.1f) {
						posTarget = posTarget + Vector3.right * -speedH * 0.02f;
					} else if (x > 0.1f) {
						posTarget = posTarget + Vector3.left * -speedH * 0.02f;
					}
				} else {
					//x小于最小值
					if (transform.position.x < minX) {
						if (x > 0.1f) {
							posTarget = posTarget + Vector3.left * -speedH * 0.02f;
						}
					}
					//x大于最小值
					else {
						if (x < -0.1f) {
							posTarget = posTarget + Vector3.right * -speedH * 0.02f;
						}
					}
				}
				//y方向在最大值和最小值
				if (transform.position.z >= minY && transform.position.z <= maxY) {
					if (y < -0.1f) {
						posTarget = posTarget + Vector3.forward * -speedV * 0.02f;
					} else if (y > 0.1f) {
						posTarget = posTarget + Vector3.back * -speedV * 0.02f;
					}
				} else {
					//y小于最小值
					if (transform.position.z < minY) {
						if (y > 0.1f) {
							posTarget = posTarget + Vector3.back * -speedV * 0.02f;
						}
					}
					//y大于最小值
					else {
						if (y < -0.1f) {
							posTarget = posTarget + Vector3.forward * -speedV * 0.02f;
						}
					}
				}
			}
		}
	}
	void LateUpdate ()
	{
		if (Vector3.Distance (posTarget, transform.position) > 0.01f) {
			transform.position = Vector3.Lerp (transform.position, posTarget, Time.deltaTime * lerpSpeed);
		} else {
			transform.position = posTarget;
		}
	}
}


你可能感兴趣的:(Unity3D)