码迷,mamicode.com
首页 > 其他好文 > 详细

camera视角控制以及定位观察

时间:2015-06-04 11:30:56      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

1.类似英雄联盟的相机控制

using UnityEngine;
using System.Collections;

public class CameraCntrl : MonoBehaviour
{
public float camSpeed = 1;
public int GUIsize = 25;
public Transform target;
public bool cameraLocked = false;

void Update()
{
if(Input.GetKeyDown(KeyCode.Y))
{
cameraLocked = !cameraLocked;
}

if(Input.GetKeyDown(KeyCode.Space))
{
ResetCameraPos();
}

if(cameraLocked == false)
{
Rect recdown = new Rect(0, 0, Screen.width, GUIsize);
Rect rectup = new Rect(0, Screen.height - GUIsize, Screen.width, GUIsize);
Rect rectleft = new Rect(0, 0, GUIsize, Screen.height);
Rect rectright = new Rect(Screen.width - GUIsize, 0, GUIsize, Screen.height);

if(recdown.Contains(Input.mousePosition))
{
transform.Translate(0, 0, -camSpeed, Space.World);
}

if (rectup.Contains(Input.mousePosition))
{
transform.Translate(0, 0, camSpeed, Space.World);
}

if (rectleft.Contains(Input.mousePosition))
{
transform.Translate(-camSpeed, 0, 0, Space.World);
}

if (rectright.Contains(Input.mousePosition))
{
transform.Translate(camSpeed, 0, 0, Space.World);
}
}

else if(cameraLocked)
{
ResetCameraPos();
}
}

void ResetCameraPos()
{
Vector3 pos = transform.position;
pos.x = target.transform.position.x;
pos.z = target.transform.position.z - 5;
transform.position = pos;
}
}

 

2.距离控制

using UnityEngine;
using System.Collections;

public class cameraZoom : MonoBehaviour
{
public float currentFov = 60;
public float sensitivity = 50;
public float damping = 5;

public float minFov = 40;
public float maxFov = 60;


void Start ()
{
currentFov = camera.fieldOfView;
}

// Update is called once per frame
void Update ()
{
currentFov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
currentFov = Mathf.Clamp(currentFov, minFov, maxFov);
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, currentFov, Time.deltaTime * damping);
}
}

camera视角控制以及定位观察

标签:

原文地址:http://www.cnblogs.com/xwwFrank/p/4551030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!