1、把鼠标的位置转化成一个射线,利用射线和场景中的物体做碰撞检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class APICamera : MonoBehaviour
{
private Camera mainCamera;
// Start is called before the first frame update
void Start()
{
//获取Main Camera的Camera组件
mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
//把鼠标在屏幕上的点转变成一个射线
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
//绘制射线
Debug.DrawRay(ray.origin, ray.direction * 10,Color.yellow);
//用来存储碰撞的游戏物体
RaycastHit hit;
//碰撞检测
bool isCollder = Physics.Raycast(ray, out hit);
//如果碰撞到,输出碰撞到的游戏物体
if (isCollder)
{
Debug.Log(hit.collider);
}
if (Input.GetKeyDown(KeyCode.Space))
{
//退出编辑器游戏
UnityEditor.EditorApplication.isPlaying = false;
}
}
}
2、实现摄像机跟随的三种方法
2.1、实现摄像机跟随_固定摄像机方法,常用于RPG游戏
在Unity的坐标系中,将摄像机固定在主角头部上边靠后位置,这样,主角在移动过程中,摄像机也随着移动。
也就是说,摄像机相对于主角,总是在Vector3.forward方向靠后一些,在Vector3.up方向偏上一些。同时为了使摄像机的移动更加平滑,避免掉帧现象,引入差值函数Vector3.Lerp(),使摄像机的移动更加圆润。
代码如下:
using UnityEngine;
using System.Collections;
/// <summary>
/// Third person camera.
/// </summary>
public class TheThirdPersonCamera : MonoBehaviour
{
public float distanceAway = 1.7f; //水平方向偏移
public float distanceUp = 1.3f; //竖直方向偏移
public float smooth = 2f; // 平滑系数
private float h;
private float v;
private Vector3 m_TargetPosition; // 摄像机目标位置
public Transform playerTrans; //玩家的位置
// Update is called once per frame
void Update()
{
// 得到目标位置
m_TargetPosition = playerTrans.position + Vector3.up * distanceUp - Vector3.forward * distanceAway;
// 从当前位置平滑的到达目标位置(摄像机相对于playerTrans位置始终保持不变)
transform.position = Vector3.Lerp(transform.position, m_TargetPosition, Time.deltaTime * smooth);
// 使摄像机的Z轴始终指向playerTrans的位置
transform.LookAt(playerTrans);
//控制playerTrans的移动
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
playerTrans.Translate(new Vector3(h, 0, v) * Time.deltaTime * 10, Space.World);
}
}
2.2、实现摄像机跟随_摄像机替代主角方法,常用于第一人称射击
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//获取玩家
public Transform p_transform;
//摄像机
private Transform p_camTransform;
private Vector3 p_camRot; //摄像机旋转角度
public float p_camHeight = 4.13f; //摄像机高度,即玩家身高
private float moveSpeed = 5.0f;
// Start is called before the first frame update
void Start()
{
//获取摄像机
p_camTransform = Camera.main.transform;
//设置摄像机的初始位置
p_camTransform.position = p_transform.TransformPoint(0, p_camHeight, 0);
//设置摄像机的方向与主角一致
p_camTransform.rotation = p_transform.rotation;
p_camRot = p_camTransform.eulerAngles;
//锁定鼠标
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Control();
}
/// <summary>
/// 在移动玩家后使摄像机的位置与玩家保持一致
/// </summary>
void Control()
{
//获取鼠标移动距离
float rh = Input.GetAxis("Mouse X");
float rv = Input.GetAxis("Mouse Y");
//旋转摄像机
p_camRot.x -= rv;
p_camRot.y += rh;
//控制鼠标旋转摄像机方向
p_camTransform.eulerAngles = p_camRot;
//使玩家的面向方向和摄像机一致
Vector3 camRot = p_camTransform.eulerAngles;
camRot.x = 0;
camRot.z = 0;
p_transform.eulerAngles = camRot;
//玩家移动
Vector3 motion = Vector3.zero;
motion.x = Input.GetAxis("Horizontal");
motion.z = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
p_transform.Translate(motion * moveSpeed * Time.deltaTime, Space.World);
//更新摄像机位置
p_camTransform.position = p_transform.TransformPoint(0, p_camHeight, 0);
}
}
2.3、实现摄像机跟随_相机始终在玩家的正后方
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform Player;
//偏移量
private Vector3 offSet;
//目标点
private Vector3 targetPosion;
//平滑系数
private float smoothing = 3f;
// Start is called before the first frame update
void Start()
{
//获取偏移量
offSet = transform.position - Player.position;
}
// Update is called once per frame
void Update()
{
//获取目标位置
targetPosion = Player.position + Player.TransformDirection(offSet);
//平滑的由当前位置到达目标位置
transform.position = Vector3.Lerp(transform.position, targetPosion, Time.deltaTime * smoothing);
transform.LookAt(Player.position);
}
}