本文使用Rigidbody控制飞机,基本不会穿模 。
效果
飞机飞行
这是一条优雅的广告
如果你也在开发飞机大战等类型的飞行游戏,欢迎在主页搜索博文并参考。
搜索词:Unity游戏(Assault空对地打击)开发。
脚本编写
首先是完整代码。
csusing System.Collections; using System.Collections.Generic; using UnityEngine; public class FlightCtrl : MonoBehaviour { private float rotationSpeed = 5f; private float rollSpeed = 1f; private float minSpeed = 40f; private float maxSpeed = 200f; private float moveSpeed = 100f; private Rigidbody rb; private void Start() { rb = GetComponent<Rigidbody>(); } private void FixedUpdate() { Move(); Yaw(); Pitch(); Roll(); } private void Move() { float move = Input.GetAxis("Vertical"); moveSpeed += move; if (moveSpeed < minSpeed) moveSpeed = minSpeed; else if (moveSpeed > maxSpeed) moveSpeed = maxSpeed; rb.velocity = transform.forward * moveSpeed; } private void Yaw() { if (Input.GetMouseButton(0)) { float yaw = Input.GetAxis("Mouse X"); Vector3 newTorque = new Vector3(0f, yaw * pitchSpeed, 0f); rb.AddRelativeTorque(newTorque); } } private void Pitch() { if (Input.GetMouseButton(1)) { float pitch = Input.GetAxis("Mouse Y"); Vector3 newTorque = new Vector3(pitch * pitchSpeed, 0f, 0f); rb.AddRelativeTorque(newTorque); } } private void Roll() { float roll = Input.GetAxis("Horizontal"); // A, D/上, 下(-1, 1) Vector3 newTorque = new Vector3(0f, 0f, -roll * rollSpeed); rb.AddRelativeTorque(newTorque); } }分块解析
rotationSpeed:后续有用,请根据实际情况来调整数值,控制偏航和俯仰的转向速度。
rollSpeed:同理,控制横滚的速度。
moveSpeed:前进的速度,初始为100f。
min/maxSpeed:最低/高速。
rb:Rigidbody组件。
csprivate float rotationSpeed = 5f; private float rollSpeed = 1f; private float minSpeed = 40f; private float maxSpeed = 200f; private float moveSpeed = 100f; private Rigidbody rb;用处:获取Rigidbody组件,用rb表示。
csprivate void Start() { rb = GetComponent<Rigidbody>(); }更新时:进行飞机的 前后移动、偏航、俯仰、横滚(如果需要操作)。
csprivate void FixedUpdate() { Move(); Yaw(); Pitch(); Roll(); }move:通常获取键盘的 W 和 S 键,或者方向键的上下箭头,W上(1)、S(下)(-1)。
加速或减速。
限制速度。
设置刚体速度:方向为物体的前方,速度为moveSpeed。
csprivate void Move() { float move = Input.GetAxis("Vertical"); moveSpeed += move; if (moveSpeed < minSpeed) moveSpeed = minSpeed; else if (moveSpeed > maxSpeed) moveSpeed = maxSpeed; rb.velocity = transform.forward * moveSpeed; }当左键按下时,执行偏航操作(左右转动)。
获取鼠标的横向位置移动(左右移动)(左-1右1)
yaw * rotationSpeed,直接用yaw力太大或太小,需要乘一个数控制大小。
给刚体添加转向方向。
csprivate void Yaw() { if (Input.GetMouseButton(0)) { float yaw = Input.GetAxis("Mouse X"); Vector3 newTorque = new Vector3(0f, yaw * rotationSpeed, 0f); rb.AddRelativeTorque(newTorque); } }以此类推。
csprivate void Pitch() { if (Input.GetMouseButton(1)) { float pitch = Input.GetAxis("Mouse Y"); Vector3 newTorque = new Vector3(pitch * rotationSpeed, 0f, 0f); rb.AddRelativeTorque(newTorque); } } private void Roll() { float roll = Input.GetAxis("Horizontal"); // A, D/上, 下(-1, 1) Vector3 newTorque = new Vector3(0f, 0f, -roll * rollSpeed); rb.AddRelativeTorque(newTorque); }
Unity飞行代码 超仿真 保姆级教程
lin zaixi()2025-02-05 10:40
相关推荐
地狱为王14 小时前
Unity使用VoxLock实现语音隔离解决小子18 小时前
【虚幻引擎】【GAS】模块详解一只大鸽子19 小时前
游戏开发笔记0-Godot启动暴走约伯21 小时前
【VFX工具链源码拆解 03】告别机械劳动(下):材质字典映射批处理与智能纹理槽识别暴走约伯1 天前
【VFX工具链源码拆解 01】总体架构:大型项目特效流程的程序化落地Oiiouui2 天前
Godot-GridGame架构DI容器全局联动设计FFFFujige3 天前
【Unity】一个的Dots用Ruhanka踩坑记录wgslucky3 天前
Godot UI管理与使用Oiiouui3 天前
Godot-GridGame架构屏幕UI层凯基迪科技4 天前
Unity游戏加密锁逆向方案、选型与攻防分析