Unity飞行代码 超仿真 保姆级教程

本文使用Rigidbody控制飞机,基本不会穿模

效果

飞机飞行

这是一条优雅的广告

如果你也在开发飞机大战等类型的飞行游戏,欢迎在主页搜索博文并参考。

搜索词:Unity游戏(Assault空对地打击)开发。

脚本编写

首先是完整代码。

cs 复制代码
using 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组件。

cs 复制代码
private float rotationSpeed = 5f;
private float rollSpeed = 1f;
private float minSpeed = 40f;
private float maxSpeed = 200f;

private float moveSpeed = 100f;
private Rigidbody rb;

用处:获取Rigidbody组件,用rb表示。

cs 复制代码
private void Start()
{
    rb = GetComponent<Rigidbody>();
}

更新时:进行飞机的 前后移动、偏航、俯仰、横滚(如果需要操作)。

cs 复制代码
private void FixedUpdate()
{
    Move();
    Yaw();
    Pitch();
    Roll();
}

move:通常获取键盘的 W 和 S 键,或者方向键的上下箭头,W上(1)、S(下)(-1)。

加速或减速。

限制速度。

设置刚体速度:方向为物体的前方,速度为moveSpeed。

cs 复制代码
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;
}

当左键按下时,执行偏航操作(左右转动)。

获取鼠标的横向位置移动(左右移动)(左-1右1)

yaw * rotationSpeed,直接用yaw力太大或太小,需要乘一个数控制大小。

给刚体添加转向方向。

cs 复制代码
private void Yaw()
{
    if (Input.GetMouseButton(0))
    {
        float yaw = Input.GetAxis("Mouse X");
        Vector3 newTorque = new Vector3(0f, yaw * rotationSpeed, 0f);
        rb.AddRelativeTorque(newTorque);
    }
}

以此类推。

cs 复制代码
private 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);
}
相关推荐
不绝1911 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
贵州晓智信息科技1 天前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
unicrom_深圳市由你创科技2 天前
Unity 的UI动画调节
ui·unity·游戏引擎
咩咩觉主2 天前
Unity编辑器拓展 IMGUI与部分Utility知识总结(代码+思维导图)
unity·c#·编辑器·游戏引擎
龚子亦2 天前
【Unity开发】数据存储——XML
xml·unity·游戏引擎·数据存储·游戏开发
write_the_code2 天前
Unity国际版下载链接分享(非c1国内版)
unity·游戏引擎
Bulling_2 天前
unity动态背景制作
unity·游戏引擎
枯萎穿心攻击3 天前
响应式编程入门教程第八节:UniRX性能分析与优化
ui·unity·架构·c#·游戏引擎
我寄人间雪满头丶4 天前
Unity使用Luna Playworks开发试玩广告问题处理
unity·游戏引擎
伽蓝_游戏4 天前
Unity UI的未来之路:从UGUI到UI Toolkit的架构演进与特性剖析(2)
游戏·ui·unity·架构·c#·游戏引擎·.net