Unity3d摄像机跟随角色移动脚本
2017-09-25 23:25
633 查看
public class CameraController : MonoBehaviour
{
public Camera cam;
public Transform hero;
public bool movable;
public float velocity = 4;
public bool follow;
private Vector3 pivot = new Vector3(0,0,0);
public bool lockCursor = false; //no mouse 1 reqired
public float elevation = 1.5f;
public float sensitivity = 1f;
private float rotationX = 0;
private float rotationY = 190;
public void Start ()
{
if (cam==null) cam = Camera.main;
//if (hero==null) hero = ((CharController)FindObjectOfType(typeof(CharController))).transform;
pivot = cam.transform.position;
}
public void LateUpdate () //updating after hero is moved and all other scene changes made
{
//locking cursor
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
//reading controls
if (Input.GetMouseButton(1) || lockCursor)
{
rotationY += Input.GetAxis("Mouse X")*sensitivity; //note that axises from screen-space to world-space are swept!
rotationX -= Input.GetAxis("Mouse Y")*sensitivity;
rotationX = Mathf.Min(rotationX, 89.9f);
}
//setting cam
if (hero!=null) pivot = hero.position + new Vector3(0, elevation, 0);
//moving
if (movable)
{
if (Input.GetKey (KeyCode.W)) pivot += transform.forward * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.S)) pivot -= transform.forward * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.D)) pivot += transform.right * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.A)) pivot -= transform.right * velocity * Time.deltaTime;
}
cam.transform.localEulerAngles = new Vector3(rotationX, rotationY, 0); //note that this is never smoothed
cam.transform.position = pivot;
}
}
{
public Camera cam;
public Transform hero;
public bool movable;
public float velocity = 4;
public bool follow;
private Vector3 pivot = new Vector3(0,0,0);
public bool lockCursor = false; //no mouse 1 reqired
public float elevation = 1.5f;
public float sensitivity = 1f;
private float rotationX = 0;
private float rotationY = 190;
public void Start ()
{
if (cam==null) cam = Camera.main;
//if (hero==null) hero = ((CharController)FindObjectOfType(typeof(CharController))).transform;
pivot = cam.transform.position;
}
public void LateUpdate () //updating after hero is moved and all other scene changes made
{
//locking cursor
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
//reading controls
if (Input.GetMouseButton(1) || lockCursor)
{
rotationY += Input.GetAxis("Mouse X")*sensitivity; //note that axises from screen-space to world-space are swept!
rotationX -= Input.GetAxis("Mouse Y")*sensitivity;
rotationX = Mathf.Min(rotationX, 89.9f);
}
//setting cam
if (hero!=null) pivot = hero.position + new Vector3(0, elevation, 0);
//moving
if (movable)
{
if (Input.GetKey (KeyCode.W)) pivot += transform.forward * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.S)) pivot -= transform.forward * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.D)) pivot += transform.right * velocity * Time.deltaTime;
if (Input.GetKey (KeyCode.A)) pivot -= transform.right * velocity * Time.deltaTime;
}
cam.transform.localEulerAngles = new Vector3(rotationX, rotationY, 0); //note that this is never smoothed
cam.transform.position = pivot;
}
}
相关文章推荐
- unity3d阶段性学习脚本代码(2个是摄像机跟随(2D游戏中的),1个是角色跳跃移动脚本)
- Unity3d相机跟随角色移动
- unity3d 摄像机跟随角色时被物体遮挡解决方案
- Unity3D 人物移动 摄像机跟随
- Unity摄像机跟随角色移动
- Unity3D Player角色移动控制脚本——转载
- Unity3D AI角色移动和自动跟随
- 【Unity快速实现小功能】动画系统学习小技巧篇(二)——摄像机跟随角色移动
- 分享一个Unity3D点击模型跟随鼠标移动的小脚本(包括屏幕视图到三维空间的坐标转换)
- Unity3D 控制角色移动,转向和自动跟随
- [脚本] Unity3d 实现鼠标左键点击地形使角色移动到指定地点
- 【Unity&摄像机&NGUI&2.5D】UILabel跟随角色移动和其参数调整
- Unity3d学习记录(一)摄像机跟随人物移动,并且注视人物
- unity 如何控制摄像机跟随角色移动
- Unity摄像机跟随玩家移动脚本
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- Unity3d 实现鼠标左键点击地形使角色移动到指定地点[脚本]
- Unity3D Player角色移动控制脚本
- Unity3D鼠标控制摄像机“左右移动控制视角+WASD键盘控制前后左右+空格键抬升高度”脚本
- Unity3D Player角色移动控制脚本