How to add wing flaps to your airplane in your aircraft unity game development



I did make a simple aircraft controller in Unity, and this time in this video I will add wing flaps to my aircraft.

Wing flaps is some small wing objects in airplane wings that somehow rotates to control the aircraft movement. In this video I will make those wing flaps and how to control them using our WASD keyboard and also by clicking our on screen buttons.

Here is the video how I did it:

https://www.youtube.com/embed/7bz6di1_ih4

And here is the TNAirplane C# script updated so far:

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

public class TNAirPlane : MonoBehaviour
{
	
	public float thrust = 0f;
	public float topspeed = 20f;
	public float defaultLift = 1f;
	public Slider thrustSlider;
	
	public GameObject leftWingFlap;
	public GameObject rightWingFlap;
	
	float goingUp, rotateRight, slideRight, dynamicLift, fallRate, glideRate;
	bool isGrounded;
	
	Rigidbody rb;
	
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
		goingUp = 0f;
		rotateRight = 0f;
		
		thrustSlider.onValueChanged.AddListener(delegate{
			ValueChangeCheck();
		});
    }

    // Update is called once per frame
    void FixedUpdate()
    {
		transform.Translate(0, 0, (thrust + (glideRate*25)) * Time.deltaTime);
		transform.Rotate(dynamicLift,0,0);
		rb.velocity = new Vector3(0, rb.velocity.y * fallRate, 0);
		
		//Down
		if(Input.GetKey(KeyCode.W) || goingUp > 0f){
			if(leftWingFlap.transform.localEulerAngles.x > 315 || leftWingFlap.transform.localEulerAngles.x < 180)
				leftWingFlap.transform.Rotate(-5f, 0, 0);
			if(rightWingFlap.transform.localEulerAngles.x > 315 || rightWingFlap.transform.localEulerAngles.x < 180)
				rightWingFlap.transform.Rotate(-5f, 0, 0);
			if(thrust > topspeed/2)
				transform.Rotate(.5f,0,0);
		}
		//Up
		else if(Input.GetKey(KeyCode.S) || goingUp < 0f){
			if(leftWingFlap.transform.localEulerAngles.x < 45 || leftWingFlap.transform.localEulerAngles.x > 180)
				leftWingFlap.transform.Rotate(5f, 0, 0);
			if(rightWingFlap.transform.localEulerAngles.x < 45 || rightWingFlap.transform.localEulerAngles.x > 180)
				rightWingFlap.transform.Rotate(5f, 0, 0);
			if(thrust > topspeed/2)
				transform.Rotate(-.5f,0,0);
		}
		//RotateLeft
		else if(Input.GetKey(KeyCode.A) || rotateRight < 0f){
			if(leftWingFlap.transform.localEulerAngles.x < 45 || leftWingFlap.transform.localEulerAngles.x > 180)
				leftWingFlap.transform.Rotate(5f, 0, 0);
			if(rightWingFlap.transform.localEulerAngles.x > 315 || rightWingFlap.transform.localEulerAngles.x < 180)
				rightWingFlap.transform.Rotate(-5f, 0, 0);
			if(thrust > topspeed/2)
				transform.Rotate(0,0,.5f);
		}
		//RotateRight
		else if(Input.GetKey(KeyCode.D) || rotateRight > 0f){
			if(leftWingFlap.transform.localEulerAngles.x > 315 || leftWingFlap.transform.localEulerAngles.x < 180)
				leftWingFlap.transform.Rotate(-5f, 0, 0);
			if(rightWingFlap.transform.localEulerAngles.x < 45 || rightWingFlap.transform.localEulerAngles.x > 180)
				rightWingFlap.transform.Rotate(5f, 0, 0);
			if(thrust > topspeed/2)
				transform.Rotate(0,0,-.5f);
		}
		//SlideLeft
		else if(Input.GetKey(KeyCode.Q) || slideRight < 0f){
			transform.Rotate(0,-.5f,0);
		}
		//SlideRight
		else if(Input.GetKey(KeyCode.E) || slideRight > 0f){
			transform.Rotate(0,.5f,0);
		}else{
			if(leftWingFlap.transform.localRotation.x < 0){
				leftWingFlap.transform.Rotate(5f, 0, 0);
			}
			if(rightWingFlap.transform.localRotation.x < 0){
				rightWingFlap.transform.Rotate(5f, 0, 0);
			}
			if(leftWingFlap.transform.localRotation.x > 0){
				leftWingFlap.transform.Rotate(-5f, 0, 0);
			}
			if(rightWingFlap.transform.localRotation.x > 0){
				rightWingFlap.transform.Rotate(-5f, 0, 0);
			}
		}
		
    }
	
	public void GoUpDown(float v){
		goingUp = v;
	}
	
	public void GoLeftRight(float v){
		rotateRight = v;
	}
	
	public void SlideLeftRight(float v){
		slideRight = v;
	}
	
	public void ValueChangeCheck(){
		thrust = thrustSlider.value * topspeed;
		dynamicLift = thrustSlider.value * defaultLift;
		fallRate = 1f - thrustSlider.value;
		if(!isGrounded)
			glideRate = 1f - thrustSlider.value;
	}
	
	void OnCollisionEnter(Collision collision){
		if(collision.gameObject.tag == "Ground"){
			isGrounded = true;
			glideRate = 0;
			
		}
	}
	
	void OnCollisionExit(Collision collision){
		if(collision.gameObject.tag == "Ground"){
			isGrounded = false;
		}
	}
	
}

Check out the complete parts of this tutorial here: https://thirteenov.ciihuy.com/making-an-airplane-game-from-scratch-in-unity/

loading...

Leave a Reply

Your email address will not be published.