4000 基于Unity3D的相机功能的实现(六)—— 上帝视角(王者荣耀视角)
2017-12-10 16:40
826 查看
在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;
}
}
}
代码如下:
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的相机功能的实现(四)——第三人称相机(TPS)
- 基于Unity3D的相机功能的实现(八)—— 收藏篇
- 基于Unity3D的相机功能的实现(五)—— 相机跟随
- 基于Unity3D的相机功能的实现(三)——第一人称相机(FPS)
- 基于Unity3D的相机功能的实现(七)—— 综合篇
- 基于Unity3D的相机功能的实现(一)——VR相机
- 基于Unity3D的相机功能的实现(二)——AR相机
- 基于Unity3D的打开Windows软键盘的功能的实现
- 王者荣耀 是基于Unity3d (.NET C#)开发的跨平台游戏
- 基于opencv的相机之特效功能实现(八)
- Unity3D 使用备忘(二) 实现相机自动漫游功能。
- unity3d实现相机对场景物体的旋转、缩放、平移查看功能、近似于scene下查看场景的效果
- 基于Unity3D的相机震动效果的实现
- 基于opencv的相机之马赛克功能实现(九)
- 基于opencv的相机之拼图功能实现(十)
- 基于opencv的相机之鬼怪贴图功能实现(十一)
- 基于Unity3D的移动端摇一摇功能的实现
- 基于opencv的相机之图像修补功能实现(十二)
- unity3d实现LOL中的相机控制功能
- Unity3D 旋转相机视角 镜头缩放 拖动相机视角 Game视窗还原Sence视窗功能