刚进入一家公司,上了4天班了,终于发现自己是那么的菜,看了老大写的一些代码。没办法看下去了,简直太强了,在这几天上班里,因为是很大的公司,感觉自己压力很大很大啊。心里很压抑。因为自己很菜。好了,先不说这些了,加油吧。坚持努力学习。这几天里老大叫我实现在手机里面实现镜头的放大缩小和移动场景,有点像海岛奇兵那样的控制镜头那样的效果,在写这些代码中。遇到了很多很多的问题。因为紧张,也因为自己基础不行。
代码如下,需要把这个脚本绑定在摄像机上:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public float ZoomSpeed = 500;
public float zoomSpeed
{
get
{
return ZoomSpeed;
}
set
{
ZoomSpeed = value;
}
}
public float MaxY = 3921;
public float maxY
{
get
{
return MaxY;
}
set
{
MaxY = value;
}
}
public float MinY = 1200;
public float minY
{
get
{
return MinY;
}
set
{
MinY = value;
}
}
// Update is called once per frame
private Vector2 lastTouchPos1 = Vector2.zero;
private Vector2 lastTouchPos2 = Vector2.zero;
void Update()
{
//这里是鼠标控制
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
var forward = Camera.main.transform.forward * ZoomSpeed * Input.GetAxis("Mouse ScrollWheel");
Camera.main.transform.position += forward;
}
//这里是在手机上手势控制
if (Input.touchCount > 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 v1 = Input.GetTouch(0).position;
Vector2 v2 = Input.GetTouch(1).position;
float distance = GetDistance(v1, v2);
Vector3 forward = Camera.main.transform.forward * distance * Time.deltaTime * 0.5f;
Vector3 newPos = Camera.main.transform.position + forward;
if (newPos.y > MaxY || newPos.y < MinY)
{
}
else
{
Camera.main.transform.position = newPos;
}
lastTouchPos1 = Input.GetTouch(0).position;
lastTouchPos2 = Input.GetTouch(1).position;
}
else
{
lastTouchPos1 = Vector2.zero;
lastTouchPos2 = Vector2.zero;
}
}
}
private float GetDistance(Vector2 pos1, Vector2 pos2)
{
if (lastTouchPos1 == Vector2.zero && lastTouchPos2 == Vector2.zero)
{
return 0.0f;
}
return (pos2 - pos1).sqrMagnitude - (lastTouchPos2 - lastTouchPos1).sqrMagnitude;
}
}
这里只是实现镜头的放大缩小,下次补上移动场景的。先加班了,大家加油吧