Resources
Skyline resources; Third party applications, Demo Scenes, Action plug-ins, Action Scripts, Presets etc any thing that can be used by Skyline

Aeroplane Script

Aeroplane Script

Postby SolarPortal » 24 Apr 2015, 19:39

Hey,
I am sure you have all seen Tattie's video of the fighter jet / Spaceship flying around.
Which is here for those that haven't seen: http://www.chi-ad.com/Skyline/Forum/viewtopic.php?f=41&t=1009

This is a flight that is not using physics. It is simply made using the entity lua library.
Here is the plane code:

EDIT: Updated code on 25/04/2015. Added Joypad events for flight :)
Code: Select all
-- | User Changeable Properties.
-- |=========================================================================================================================
minSpeed = 80;       -- Min speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower
maxSpeed = 120;    -- Max speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower

vBoost = 80;       -- Use to rotate the plane on the pitch(up/down) quicker. Larger = faster.
hBoost = 130;       -- Use to rotate the plane on the roll(left/right) quicker. Larger = faster.

reactionSpeed = 5;    -- This changes how fast you can cahnge direction when another key is pressed. Larger = Firmer. Smaller = Lazier

-- | Required Script Variables. (Globals)
-- |=========================================================================================================================
obj = 0;
speed = 0;
wKeyDown = 0; aKeyDown = 0; sKeyDown = 0; dKeyDown = 0;
cVertTurn = 0;
cHorTurn = 0;
joyPadPitch = 0;
joyPadRoll = 0;
-- | Airplane script
-- |=========================================================================================================================

function onInit(objID)
   obj = objID;
end

function onUpdate( timeDelta )
   -- Fetch inputs and get smooth values
   doInput( timeDelta );
   
   -- Propel the plane forwards
   doMovement( timeDelta );
   
   -- Apply Pitch & Roll to control the plane and make it turn corners.
   doRotation( timeDelta );
   
   -- Stop the plane from going through the terrain floor.
   checkGroundClearance();
end

function doMovement( timeDelta )
   -- get the normalised(0-1 range) forward direction of the plane.
   forward = newType.vec3 (vector3.normalisedCopy( entity.getForward(obj) ) );
   
   -- set the speed of the plane, slower going up and faster pointing down for gravity effect.
   speed = speed - forward.y * timeDelta * 50.0;
   if(speed < minSpeed)then speed = minSpeed; end
   if(speed > maxSpeed)then speed = maxSpeed; end
   
   -- Now move the entity forwards by the speed and using timedelta for the same flight at all framerates
   entity.move(obj, (forward.x * speed) * timeDelta, (forward.y * speed)  * timeDelta, (forward.z * speed) * timeDelta )
end

function doRotation( timeDelta )
   entity.turn(obj, cVertTurn * vBoost * timeDelta, 0, cHorTurn * hBoost * timeDelta);
end

function checkGroundClearance()
   -- Keep plane above terrain ground.
   currentPos = newType.vec3(entity.getPosition(obj));
   terrainheight = terrain.getHeightAtPoint( currentPos.x, currentPos.z );
   if( currentPos.y < terrainheight )then
      entity.setPosition( obj, currentPos.x, terrainheight, currentPos.z );
   end   
end

function doInput( timeDelta )
   -- Grab the input values and smooth them out for nicer control
   smoothRate = reactionSpeed * timeDelta;
   
   -- Plane pitching Up/Down input
   if(wKeyDown == 1)then   
      cVertTurn = cVertTurn - smoothRate;
   elseif(sKeyDown == 1)then   
      cVertTurn = cVertTurn + smoothRate;
   elseif(joyPadPitch > 0.1)then
      cVertTurn = ( cVertTurn - smoothRate ) * joyPadPitch;
   elseif(joyPadPitch < -0.1)then
      cVertTurn = ( cVertTurn + smoothRate ) * -joyPadPitch;
   else
      if(cVertTurn > 0.1)then
         cVertTurn = cVertTurn - smoothRate;   
      elseif(cVertTurn < -0.1)then
         cVertTurn = cVertTurn + smoothRate;
      else
         cVertTurn = 0;
      end
   end
   
   if(cVertTurn > 1)then cVertTurn = 1; end
   if(cVertTurn < -1)then cVertTurn = -1; end
   
   -- Plane Banking rolling input
   if(aKeyDown == 1)then
      cHorTurn = cHorTurn - smoothRate;
   elseif(dKeyDown == 1)then
      cHorTurn = cHorTurn + smoothRate;
   elseif(joyPadRoll > 0.1)then
      cHorTurn = ( cHorTurn - smoothRate ) * joyPadRoll;
   elseif(joyPadRoll < -0.1)then
      cHorTurn = ( cHorTurn + smoothRate ) * -joyPadRoll;
   else
      if(cHorTurn > 0.1)then
         cHorTurn = cHorTurn - smoothRate;
      elseif(cHorTurn < -0.1)then
         cHorTurn = cHorTurn + smoothRate;
      else
         cHorTurn = 0;
      end
   end
   
   if(cHorTurn > 1)then cHorTurn = 1; end
   if(cHorTurn < -1)then cHorTurn = -1; end
end


-- | Input key events
-- |=========================================================================================================================

function onKeyDown( key )
   if(key == "w")then   wKeyDown = 1; end
   if(key == "a")then   aKeyDown = 1; end
   if(key == "s")then   sKeyDown = 1; end
   if(key == "d")then   dKeyDown = 1; end
end

function onKeyUp( key )
   if(key == "w")then   wKeyDown = 0; end
   if(key == "a")then   aKeyDown = 0; end
   if(key == "s")then   sKeyDown = 0; end
   if(key == "d")then   dKeyDown = 0; end
end


---------------------------------------------------------------------------------
-- INPUT: GAMEPAD
---------------------------------------------------------------------------------
function onJSAxisMoved(axis, value)
   if(axis==0) then
      if(value<-400 )then
         joyPadPitch = 1;
      elseif(value > 400)then
         joyPadPitch = -1;
      else
         joyPadPitch = 0;
      end
      
   end

   if(axis==1) then
      if(value<-400 )then
         joyPadRoll = 1;
      elseif(value > 400)then
         joyPadRoll = -1;
      else
         joyPadRoll = 0;
      end
   end
end



This is a more advanced version of the script which has:
  • smooth pitch and roll effects.
  • min / max speed
  • When you fly up, the plane slows down. When you fly down, the speed increases steadily to max speed which makes it seem as though gravity is affecting it. Use a lazy cam to get cool movements.
  • You cannot fly through the terrain ground. Normal entities you can as this is NOT physics based.
  • multiple adjustment properties to help make it flyable.
  • Fully timedelta'd. This means it will fly the same at 30 fps as it would at 500 fps.
  • Keys do not stop working like the previous code did.
We are still working on this and will keep this thread updated. Hoping to get Joypad controls working too :D
Look forward to seeing some more videos from Tattie ;) and any other work people make using this script.

To get this working:
> Add your mesh
> Add a microscript or external script and add the code from above.
> Press play and fly around. :)

Enjoy!
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3179
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Aeroplane Script

Postby SolarPortal » 25 Apr 2015, 21:19

Hi guys, got another update for this. Just had a spare 10 minutes and added Joypad (Xbox 360 controller) control to the plane.
Now the left stick will pitch and roll the plane. It does get a bit to get used to, but feels pretty good.

The code in the first post has been updated. :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3179
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Aeroplane Script

Postby TattieBoJangle » 25 Apr 2015, 21:30

Thanks! :D
Case: CM Storm Trooper CPU: I7 5930k X99 Cooler: Noctua NH-D15 Graphics: Asus GTX 1080 Motherboard: Rampage Extreme V x99 Ram: RipJaws DDR4 3000mhz Storage: x2 SSD Crucial 500GB + x5 2TB Hdd PSU: Evga 1500w OS: Windows 10
User avatar
TattieBoJangle
Community Manager
Community Manager
 
Posts: 858
Joined: 26 Jan 2015, 00:15
Location: United Kingdom
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer
Skill: Great creative

Re: Aeroplane Script

Postby Lewis-H » 05 Mar 2020, 13:24

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added.

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLookScript : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;

public float sensitivityX = 10F;
public float sensitivityY = 10F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -60F;
public float maximumY = 60F;

float rotationY = 0F;

void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

//lateUpdate(); ?
void Update ()
{

if ((axes == RotationAxes.MouseXAndY) && (GlobalDataScript.cursorState==1))
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if ((axes == RotationAxes.MouseX) && (GlobalDataScript.cursorState==1))
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else if (GlobalDataScript.cursorState==1)
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
This code for other controls:

int flyingSpeed = 100;
int speedChange = 20;

int rotationInt = 2;

Update()
{

if (Input.GetKeyDown (KeyCode.W))
{
flyingSpeed += speedChange;
}

if (Input.GetKeyDown (KeyCode.S))
{
flyingSpeed += -speedChange;
}

if (Input.GetKey (KeyCode.A))
{
transform.RotateAround (transform.position, transform.up, -rotateInt);
}

if (Input.GetKey (KeyCode.D))
{
transform.RotateAround (transform.position, transform.up, rotateInt);
}
}
FixedUpdate() {

transform.position += transform.forward flyingSpeed Time.deltaTime;

}

You might have to adjust some variables like the speed, the speed change, and the rotationInt to get it to fly how you want.

If you apply these scripts to the plane gameObject and set the camera to be the central transform position this work. Just make sure if you are using a rigid body you turn off the gravity.

I'm not sure how well this will work like I said I never made a flying game like this. But this should at least get you started.
Lewis-H
Skyline Novice
 
Posts: 6
Joined: 05 Mar 2020, 13:15
Skill: Programmer
Skill: Programmer
Skill: Programmer
Skill: Programmer
Skill: Programmer


Return to Resources

Who is online

Users browsing this forum: No registered users and 1 guest