Unity模拟3D飞机驾驶(稳定版和灵活版)

1、效果呈现: 

b站视频链接:https://www.bilibili.com/video/BV1cU4y1m7jE?share_source=copy_web

注:视频中的实现均纯代码,无动画。

2、操作说明:

自动驾驶操作:AD控制左右旋转,WS控制升降,飞机自动往前行驶。此驾驶模式飞机更平稳,飞机自动回位。

手动驾驶操作:鼠标控制旋转升降,按住X飞机往前行驶。此驾驶模式飞机更灵活。

按Tap键切换驾驶模式。

3、效果实现:

Game窗口分辨率:1920*1080。

Hierarchy面板设置,Player空物体挂PlayerControl脚本(源码在下面)和Rigidbody,在空物体下挂模型Model,以Model命名(脚本中会找这个Model),场景中的相机挂在此物体下。 

Unity模拟3D飞机驾驶(稳定版和灵活版)_第1张图片

底下是源码,纯手撸,创作不易,点个赞谢谢谢谢谢!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    private Transform model;
    private Rigidbody rb;
    private float moveSpeed = 0;//自动驾驶行驶速度
    private float getAxisAD;//键a,d控制左右 horizontal
    private float getAxisWS;//键w,s控制升降 vertical
    private float xRotation;
    private int rSpeed = 3;
    private int maxHAngle = 70;//水平翻转最大角度
    private int maxVAngle = 30;//数直翻转最大角度
    private float smoothing = 2;
    private bool isHandCtr = false;

    //以下是自动驾驶的参数
    float xPosPercent=0;
    float yPosPercent=0;
    private void Awake()
    {
        model=transform.Find("Model");
        rb = this.GetComponent();
    }
    private void FixedUpdate()
    {
        if(isHandCtr){
            AddSpeed();
            
        }else{
            moveSpeed = Mathf.Lerp(moveSpeed, 10, Time.deltaTime * 2);
        }
        rb.velocity = transform.forward * moveSpeed;
    }
    private void Update()
    {
        SwitchCtr();
        if (isHandCtr)
        {
            HandCtrl();
        }
        else
        {
            AutoCtrl();
        }
    }
    private void SwitchCtr()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            isHandCtr = !isHandCtr;
            Restitution();
        }
    }
    /// 
    /// 加速
    /// 
    private void AddSpeed()
    {
        if (Input.GetKey(KeyCode.X))
        {
            moveSpeed = Mathf.Lerp(moveSpeed, 15, Time.deltaTime * 2);
        }
        else
        {
            //moveSpeed = Mathf.Lerp(moveSpeed, 10, Time.deltaTime * 2);
            moveSpeed = Mathf.Lerp(moveSpeed, 0, Time.deltaTime * 2);
        }
    }
    private void AutoCtrl()
    {
        InputSmoothing();
        AutoTurnModel();
        AutoRiseOrDown();
        AutoLeftOrRight();
    }
    /// 
    /// 自动驾驶调参
    /// 
    private void InputSmoothing()
    {
        getAxisAD = Mathf.Lerp(getAxisAD, Input.GetAxis("Horizontal"), Time.deltaTime * smoothing);//ad
        getAxisWS = Mathf.Lerp(getAxisWS, Input.GetAxis("Vertical"), Time.deltaTime * smoothing);//ws
    }
    /// 
    /// 自动驾驶飞机升降
    /// 
    private void AutoRiseOrDown()
    {
        xRotation = Mathf.Lerp(xRotation, 30 * Input.GetAxis("Vertical"), Time.deltaTime * rSpeed);
    }
    /// 
    /// 自动驾驶飞机左右旋转
    /// 
    private void AutoLeftOrRight()
    {
        transform.rotation = Quaternion.Euler(new Vector3(-xRotation, transform.localEulerAngles.y + Input.GetAxis("Horizontal"), 0));
    }
    /// 
    /// 自动驾驶调模型偏转
    /// 
    private void AutoTurnModel()
    {
        model.localEulerAngles = new Vector3(-getAxisWS * maxVAngle, model.localEulerAngles.y, -getAxisAD * maxHAngle);
    }
    private void HandCtrl()
    {
        SetPercent();
        HandTurnModel();
        HandRiseOrDown();
        HandLeftOrRight();
    }
    /// 
    /// 手动驾驶调参
    /// 
    private void SetPercent(){
        xPosPercent = ((Input.mousePosition.x / Screen.width)-0.5f)*2;//获取鼠标横轴的百分比
        yPosPercent = (Input.mousePosition.y / Screen.height)-0.3f;//获取鼠标纵轴的百分比
    }
    /// 
    /// 手动驾驶调模型偏转
    /// 
    private void HandTurnModel(){
        model.localEulerAngles = new Vector3(-yPosPercent * maxVAngle, model.localEulerAngles.y, -xPosPercent * maxHAngle);
    }
    /// 
    /// 手动驾驶飞机升降
    /// 
    private void HandRiseOrDown(){
        xRotation = Mathf.Lerp(xRotation, 60 * yPosPercent, Time.deltaTime * rSpeed);
    }
    /// 
    /// 手动驾驶飞机左右旋转
    /// 
    private void HandLeftOrRight(){
        transform.rotation = Quaternion.Euler(new Vector3(-xRotation, transform.localEulerAngles.y + xPosPercent, 0));
    }
    private void Restitution(){
        xPosPercent=0;
        yPosPercent=0;
        getAxisAD=0;
        getAxisWS=0;
    }
}

你可能感兴趣的:(unity,c#)