"Upwind Zone"

Hi all,…

I have some great ideas for a flying game. I am ABSOLUTELY new to Unity and coding. I am already in love with Unity and had some success with my first tries but basic scripting is hard for a beginner like me :wink: I am trying to learn javascript at CodeAcademy but it goes along slowly and sluggishly.

Until I learn to create something similar myself I have used the Flight Sim Starter Script found here:
http://forum.unity3d.com/threads/42616-Flight-Sim-Starter-Script-(Files-Included)
for my first prototype.

It works like a charm - I added the Fighter prefab to my level, changed the Input Settings and … voila… I’m flying… GREAT :smile:

So far so good.

Now I want to make an “Upwind Zone” where an extra constant liftforce is added to my rigidbody = fighter prefab in a specific zone. Some sort of thermal upwind.

I searched and seached, watched videos and tried… but nothing works :frowning:

I created the zone as a simple cube with “Use as Trigger” enabled. I tried using OnTriggerEnter or OnTriggerStay with rigidbody.Addforce. I have understood I have to add the force in FixedUpdate but I just don’t get it…

Could some kind soul plz help me get on the right track? I know I should post a script, but I’m quite clueless as nothing has worked so far.

'm sorry for being a newbie and I don’t want to just “let someone” do the scripting for me…

But I’m honestly stuck and really need a small success just to push me in the right direction :wink:

I was too lazy to look at your simulator script, but if your plane prefab is a rigidbody driven by physics forces on your controller script you could easily check for the “Upwind zone” by using a trigger and OnTriggerEnter/Exit then applying a rigidbody.AddForce in the Vector3.Up axis.

So in ‘OnTriggerEnter’, check to make sure it’s an upwind collider. If it is, set some boolean value (upwindZone maybe?) on your flier to true. In the FixedUpdate, if the upwindZone value is true, add some force in the Up direction.

Thx a lot :slight_smile: I really think I get it but no success yet :frowning:

I tried to apply the following idea to my Flight Sim script:

… but it doesn’t work. I’m not sure I added the code correctly to the fighter movement script:

Here is the entire script with the added code marked as //---------------------------- ADDED CODE:

/*
This is a 3D First Person Controller script that can be used to simulate anything from 
high speed aerial combat, to submarine warfare. I have tried to make my variable names
as descriptive as possible so you can immediately see what they affect. The variables are
sorted by type; speed with speed, rotation with rotation ect... I have also included generic
ranges on variables that need them; relative upper and lower variable limits. This is because 
some of the variables have a greater effect as they approach 1, while others have greater
impact as they approach infinity. If for some reason the ship is not doing what it is supposed to,
check the ranges, as some variables create problems when they are set to 0 or very large values.

Also note the separate script titled Space Flight Script. This script has been
optimized to better suit space combat. The effects of gravity, drag and lift are removed
to better simulate flight in zero-gravity space.

This script uses controls based off 4 axis. I found these parameters worked well...
	Name : (Roll, Pitch, Yaw or Throttle)
	Gravity : 20
	Dead : 0.001
	Sensitivity : 1

Axis for each control (Axis based off a standard flight joystick).
	Pitch: Y- Axis
	Roll: X - Axis
	Yaw: 3'rd Axis
	Throttle: 4'th - Axis

How to use this script: 

	Drag and Drop the Transform and its Rigidbody onto the variables Flyer and
	FlyerRigidbody in the inspector panel. Remember to change the rigidbody's Drag
	value. If you dont change this, gravity will be unrealistic... (I set drag to 500)

	Change the variables to simulate the flight style you desire.

	Create a prefab of your GameObject, and back it up to a secure location.

	*Note: This is important because none of the variables are stored in the 
	script. If for some reason Unity crashes during testing, the variables
	are not stored when you save the javascript, but when you save the game
	project.

	Save often and enjoy!

	~Mirage~
*/


// Componants
var flyer : Transform;
var flyerRigidbody : Rigidbody;
var seaLevelTransform : Transform;


// Assorted control variables. These mostly handle realism settings, change as you see fit.
	var accelerateConst: float = 5;				// Set these close to 0 to smooth out acceleration. Don't set it TO zero or you will have a division by zero error.
	var decelerateConst: float = 0.065;	// I found this value gives semi-realistic deceleration, change as you see fit.
	
	/*	The ratio of MaxSpeed to Speed Const determines your true max speed. The formula is maxSpeed/SpeedConst = True Speed. 
		This way you wont have to scale your objects to make them seem like they are going fast or slow.
		MaxSpeed is what you will want to use for a GUI though.
	*/ 
	static var maxSpeed : float = 100;		
	var speedConst : float = 50;
	
	var throttleConst : int = 50;
	var raiseFlapRate : float = 1;						// Smoother when close to zero
	var lowerFlapRate : float = 1;						// Smoother when close to zero
	var maxAfterburner : float = 5;				// The maximum thrust your afterburner will produce
	var afterburnerAccelerate : float = 0.5;
	var afterburnerDecelerate : float = 1;
	var liftConst : float = 7.5;					// Another arbitrary constant, change it as you see fit.
	var angleOfAttack : float = 15;			// Effective range: 0 <= angleOfAttack <= 20
	var gravityConst = 9.8;				// An arbitrary gravity constant, there is no particular reason it has to be 9.8...
	var levelFlightPercent : int = 25;
	var maxDiveForce : float = 0.1;
	var noseDiveConst : float = 0.01;
	var minSmooth : float = 0.5;
	var maxSmooth : float = 500;
	var maxControlSpeedPercent : float = 75;		// When your speed is withen the range defined by these two variables, your ship's rotation sensitivity fluxuates.
	var minControlSpeedPercent : float = 25;		// If you reach the speed defined by either of these, your ship has reached it's max or min sensitivity.


// Rotation Variables, change these to give the effect of flying anything from a cargo plane to a fighter jet.
	var lockRotation : boolean;		// If this is checked, it locks pitch roll and yaw constants to the var rotationConst.
	var lockedRotationValue : int = 120;
	var pitchConst = 100;
	var rollConst = 100;
	var yawConst = 100;

// Airplane Aerodynamics - I strongly reccomend not touching these...
	private var nosePitch : float;
	private var trueSmooth : float;
	private var smoothRotation : float;
	private var truePitch : float;
	private var trueRoll : float;
	private var trueYaw : float;
	private var trueThrust : float;
	static var trueDrag : float;
	
// Misc. Variables
	static var afterburnerConst : float;
	static var altitude : int;

	
// HUD and Heading Variables. Use these to create your insturments.
	static var trueSpeed : int;
	static var attitude : int;
	static var incidence : int;
	static var bank : int;
	static var heading : int;

//---------------------------- ADDED CODE

private var myRigid : Rigidbody ; //used to cache the planes rigidbody
private var inWind : boolean = false ;
@HideInInspector
var windScript : WindScript ;
var windObjectTag : String = "Upwind" ; //set this in the inspector to
                                                  //whatever you tag your wind object as

//---------------------------- ADDED CODE END

// Let the games begin!
function Start ()
{
	trueDrag = 0;
	afterburnerConst = 0;
	smoothRotation = minSmooth + 0.01;
	if (lockRotation == true)
		{
		pitchConst = lockedRotationValue;
		rollConst = lockedRotationValue;
		yawConst = lockedRotationValue;
		Screen.showCursor = false;
	}
		
//---------------------------- ADDED CODE

myRigid = rigidbody ;

//---------------------------- ADDED CODE END
	


}



function Update () 
{

	// * * This section of code handles the plane's rotation.
	
	var pitch = -Input.GetAxis ("Pitch") * pitchConst;
	var roll = Input.GetAxis ("Roll") * rollConst;
	var yaw = -Input.GetAxis ("Yaw") * yawConst;

	pitch *=  Time.deltaTime;
	roll *= -Time.deltaTime;
	yaw *= Time.deltaTime;
	
	// Smothing Rotations...	
	if ((smoothRotation > minSmooth)(smoothRotation < maxSmooth))
	{
		smoothRotation = Mathf.Lerp (smoothRotation, trueThrust, (maxSpeed-(maxSpeed/minControlSpeedPercent))* Time.deltaTime);
	}
	if (smoothRotation <= minSmooth)
	{
		smoothRotation = smoothRotation +0.01;
	}
	if ((smoothRotation >= maxSmooth) (trueThrust < (maxSpeed*(minControlSpeedPercent/100))))
	{
		smoothRotation = smoothRotation -0.1;
	}
	trueSmooth = Mathf.Lerp (trueSmooth, smoothRotation, 5* Time.deltaTime);
	truePitch = Mathf.Lerp (truePitch, pitch, trueSmooth * Time.deltaTime);
	trueRoll = Mathf.Lerp (trueRoll, roll, trueSmooth * Time.deltaTime);
	trueYaw = Mathf.Lerp (trueYaw, yaw, trueSmooth * Time.deltaTime);



	
// * * This next block handles the thrust and drag.
	var throttle = (((-(Input.GetAxis ("Throttle"))+1)/2)*100);
	

	if ( throttle/speedConst >= trueThrust)
	{
		trueThrust = Mathf.SmoothStep (trueThrust, throttle/speedConst, accelerateConst * Time.deltaTime);
	}
	if (throttle/speedConst < trueThrust)
	{
		trueThrust = Mathf.Lerp (trueThrust, throttle/speedConst, decelerateConst * Time.deltaTime);
	}	

	rigidbody.drag = liftConst*((trueThrust)*(trueThrust));
	
	if (trueThrust <= (maxSpeed/levelFlightPercent))
	{
		
		nosePitch = Mathf.Lerp (nosePitch, maxDiveForce, noseDiveConst * Time.deltaTime);
	}
	else
	{
		
		nosePitch = Mathf.Lerp (nosePitch, 0, 2* noseDiveConst * Time.deltaTime);
	}
	
	trueSpeed = ((trueThrust/2)*maxSpeed);
	
// ** Additional Input

	// Airbrake
	if (Input.GetButton ("Airbrake"))
	{
		trueDrag = Mathf.Lerp (trueDrag, trueSpeed, raiseFlapRate * Time.deltaTime);		
		
	}
	
	if ((!Input.GetButton ("Airbrake"))(trueDrag !=0))
	{
		trueDrag = Mathf.Lerp (trueDrag, 0, lowerFlapRate * Time.deltaTime);
	}
	
	
	// Afterburner
	if (Input.GetButton ("Afterburner"))
	{
		afterburnerConst = Mathf.Lerp (afterburnerConst, maxAfterburner, afterburnerAccelerate * Time.deltaTime);		
	}
	
	if ((!Input.GetButton ("Afterburner"))(afterburnerConst !=0))
	{
		afterburnerConst = Mathf.Lerp (afterburnerConst, 0, afterburnerDecelerate * Time.deltaTime);
	}
	
	
	// Adding nose dive when speed gets below a percent of your max speed	
	if ( ((trueSpeed - trueDrag) + afterburnerConst) <= (maxSpeed*levelFlightPercent/100))
	{
		noseDiveConst = Mathf.Lerp (noseDiveConst,maxDiveForce, (((trueSpeed - trueDrag) + afterburnerConst) - (maxSpeed * levelFlightPercent/100)) *5 *Time.deltaTime);
		flyer.Rotate(noseDiveConst,0,0,Space.World);		
	}
	
	
	// Calculating Flight Mechanics. Used mostly for the HUD.
	attitude = parseInt(-((Vector3.Angle(Vector3.up, flyer.forward))-90));
	bank = parseInt(((Vector3.Angle(Vector3.up, flyer.up))));	
	incidence = attitude + angleOfAttack;
	heading = parseInt(flyer.eulerAngles.y);
	
	if ( seaLevelTransform != null )
	{
	altitude = (flyer.transform.position.y - seaLevelTransform.transform.position.y);
	}
	
		
	//Debug.Log ((((trueSpeed - trueDrag) + afterburnerConst) - (maxSpeed * levelFlightPercent/100)));
}	// End function Update( );


function FixedUpdate () 
{
	if (trueThrust <= maxSpeed)
	{
		// Horizontal Force
		transform.Translate(0,0,((trueSpeed - trueDrag)/100 + afterburnerConst));
	}
	
	flyerRigidbody.AddForce (0,(rigidbody.drag - gravityConst),0);
	transform.Rotate (truePitch,-trueYaw,trueRoll);
	
//---------------------------- ADDED CODE

	if(inWind  windScript!=null){ //if we're in the wind
     BlowMe(); }

//---------------------------- ADDED CODE END
	
	}// End function FixedUpdateUpdate( )


//---------------------------- ADDED CODE

//if you have on ontriggerenter() on your plane already, just add this to it
function OnTriggerEnter(hit : Collider){
   if(hit.collider.CompareTag(windObjectTag)){
      inWind = true ;
      windScript = hit.gameObject.GetComponent(WindScript) as WindScript ;
   }
}
 
//if you have ontriggerexit on your plane already, add this to that function
function OnTriggerExit(hit : Collider){
   if(hit.collider.CompareTag(windObjectTag)){
      inWind = false ;
      windScript = null ;
   }
}

function BlowMe(){
   myRigid.AddForce(windScript.wind) ; //add force from the windscript

//---------------------------- ADDED CODE END

I added a WindScript.js

var wind : Vector3 ;

…added the script to a cube tagged “Upwind” with boxcollider set to trigger…and set values for the wind.

It simply doesn’t work. What am I doing wrong?

No compiler errors though :slight_smile:

Add this script to your trigger component

public float strength = 20f;

void OnTriggerStay(Collider other){
    other.rigidbody.AddForce(Vector3.up*strength)
}

You have to make sure that your collider is set as a trigger, and always double check the strength value as engine driven flying machines tend to weight a bit…