这里的功能是使用WASD移动物体自身,物体朝向自身移动的方向,并且在移动时能够自动根据地面斜坡(角度)调整自身角度
物体的移动方向也是基于摄像机的方向。
这里是演示效果:
我这里做的物体移动时贴合地面,无论地面是平的还是斜的。下面是代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Transform _cam;//控制视野方向的摄像机
private Vector3 planeNormal;//物体移动的地面的垂直向量
private Vector3 _forward;//摄像机平行于地面的前边方向
private Vector3 _right;//摄像机平行于地面的右边方向
public float MoveSpeed;//物体移动速度
private void Start()
{
_cam = Camera.main.transform;
}
private void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
GetPlaneNormal();
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.LookRotation(_forward,transform.up); //物体基于自身Y朝向摄像机方向,下同
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.LookRotation(-_forward,transform.up);
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.LookRotation(-_right,transform.up);
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.LookRotation(_right,transform.up);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)) //摄像机左上方方向
{
transform.rotation = Quaternion.LookRotation(_forward - _right,transform.up);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.LookRotation(_forward + _right,transform.up);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.LookRotation(-_forward - _right,transform.up);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.LookRotation(-_forward + _right,transform.up);
}
transform.Translate(transform.forward * Mathf.Abs(v)*MoveSpeed * Time.deltaTime + transform.forward * Math.Abs(h)*MoveSpeed * Time.deltaTime,Space.World);
}
/// <summary>
/// 获取摄像机方向在物体移动地面的投影方向,这个方向始终平行于地面。
/// </summary>
public void GetPlaneNormal()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit, Mathf.Infinity,LayerMask.GetMask("Floor")))
{
planeNormal = hit.normal;
transform.position = hit.point+Vector3.up*transform.localScale.y/2;//设置物体在地面的位置,根据物体的中心点计算
_forward = Vector3.ProjectOnPlane(_cam.forward, planeNormal);
_right = Vector3.ProjectOnPlane(_cam.right, planeNormal);
}
}
}
脚本挂在要移动的物体上,懒得优化代码。。。
欢迎加群:4364930讨论。