Unity移动---使用WASD控制物体朝摄像机方向移动和旋转,并且自动贴合斜坡角度

这里的功能是使用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讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值