camera视角控制以及定位观察

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);
}
}

转载于:https://www.cnblogs.com/xwwFrank/p/4551030.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值