实现效果
实现思路
- 创建Cube作为Player,场景拥有两个摄影机。一个(MainCamera)跟随玩家作为主视角,另一个(Camera1)作为我们的小地图从高空俯视角度跟随玩家
- 实现两个脚本,一个支持玩家移动,另一个实现摄像机跟随玩家
步骤
基础操作详见官方文档(链接)
- 1.新建两个脚本(cameraFollow) 和(move):
move
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public Camera firstPersonCamera;
public Camera overheadCamera;
public bool status;
public float moveSpeed;
public bool zooming;
public float zoomSpeed;
// Start is called before the first frame update
void Start()
{
status = true;
moveSpeed = 50;
}
// Update is called once per frame
void Update()
{
// 玩家移动
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, 0, verticalInput) * moveSpeed * Time.deltaTime);
// 键盘k按键控制小地图显示
if (Input.GetKeyUp("k")) {
if (status == true) {
ShowOverheadView();
status = false;
} else {
ShowFirstPersonView();
status = true;
}
}
}
public void ShowOverheadView() {
firstPersonCamera.enabled = false;
overheadCamera.enabled = true;
}
public void ShowFirstPersonView() {
firstPersonCamera.enabled = true;
overheadCamera.enabled = false;
}
}
cameraFollow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraFollow : MonoBehaviour
{
public Transform playerTransform;//跟踪的对象(Cube)
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - playerTransform.position;//计算相对距离
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + offset; //保持相对距离
}
}
-
2.创建两个摄像机配置参考图片:
-
3.创建Cube(Player)配置参考图片: