1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Unity移动---使用WASD控制物体朝摄像机方向移动和旋转 并且自动贴合斜坡角度

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

时间:2020-06-29 08:17:30

相关推荐

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讨论。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。