ShipControls

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Boo - ShipControls.boo)
Line 149: Line 149:
 
== Boo - ShipControls.boo ==
 
== Boo - ShipControls.boo ==
 
This is a direct, verbatim conversion from the C# version. No attempt has been made to use Boo features.
 
This is a direct, verbatim conversion from the C# version. No attempt has been made to use Boo features.
<python>
+
<boo>
 
import UnityEngine
 
import UnityEngine
 
import System.Collections
 
import System.Collections
Line 249: Line 249:
 
rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime)
 
rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime)
 
rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime)
 
rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime)
</python>
+
</boo>

Revision as of 15:18, 2 December 2005

Author: Jonathan Czeck (aarku)

Contents

Description

This script performs 2D spaceship controls like OverWhelmed Arena that you can tweak to your heart's content. The input to the script, player or bot, is abstracted. This means you can have a Bot script in conjunction with this one that will drive the ship.

Usage

Place this script on a GameObject with a RigidBody component on it.

C# - ShipControls.cs

<csharp> using UnityEngine; using System.Collections;

// Put this on a rigidbody object and instantly // have 2D spaceship controls like OverWhelmed Arena // that you can tweak to your heart's content.


public class ShipControls : MonoBehaviour {

   public float hoverHeight = 3F;
   public float hoverHeightStrictness = 1F;
   public float forwardThrust = 5000F;
   public float backwardThrust = 2500F;
   public float bankAmount = 0.1F;
   public float bankSpeed = 0.2F;
   public Vector3 bankAxis = new Vector3(-1F, 0F, 0F);
   public float turnSpeed = 8000F;
   
   public Vector3 forwardDirection = new Vector3(1F, 0F, 0F);
   public float mass = 5F;
   
   // positional drag
   public float sqrdSpeedThresholdForDrag = 25F;
   public float superDrag = 2F;
   public float fastDrag = 0.5F;
   public float slowDrag = 0.01F;
   
   // angular drag
   public float sqrdAngularSpeedThresholdForDrag = 5F;
   public float superADrag = 32F;
   public float fastADrag = 16F;
   public float slowADrag = 0.1F;
   
   public bool playerControl = false;
   
   float bank = 0F;
   
   void SetPlayerControl(bool control)
   {
       playerControl = control;
   }
   void Start()
   {
       rigidbody.mass = mass;
   }
   
   void FixedUpdate()
   {
       if (Mathf.Abs(thrust) > 0.01F)
       {
           if (rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag)
               rigidbody.drag = fastDrag;
           else
               rigidbody.drag = slowDrag;
       }
       else
           rigidbody.drag = superDrag;
                   
       if (Mathf.Abs(turn) > 0.01F)
       {
           if (rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag)
               rigidbody.angularDrag = fastADrag;
           else
               rigidbody.angularDrag = slowADrag;
       }
       else
           rigidbody.angularDrag = superADrag;
       
       transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness);
       
       float amountToBank = rigidbody.angularVelocity.y * bankAmount;
       bank = Mathf.Lerp(bank, amountToBank, bankSpeed);
       
       Vector3 rotation = transform.rotation.ToEulerAngles();
       rotation.x = 0F;
       rotation.z = 0F;
       rotation += bankAxis * bank;
       transform.rotation = Quaternion.EulerAngles(rotation);
   }
   
   float thrust = 0F;
   float turn = 0F;
   
   void Thrust(float t)
   {
       thrust = Mathf.Clamp(t, -1F, 1F);
   }
   
   void Turn(float t)
   {
       turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed;
   }
   
   bool thrustGlowOn = false;
   
   void Update ()
   {
       float theThrust = thrust;
       
       if (playerControl)
       {
           thrust = Input.GetAxis("Thrust");
           turn = Input.GetAxis("Turn") * turnSpeed;
       }
               
       if (thrust > 0F)
       {
           theThrust *= forwardThrust;
           if (!thrustGlowOn)
           {
               thrustGlowOn = !thrustGlowOn;
               BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
           }
       }
       else
       {
           theThrust *= backwardThrust;
           if (thrustGlowOn)
           {
               thrustGlowOn = !thrustGlowOn;
               BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
           }
       }
       
       rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime);
       rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime);
   }

} </csharp>

Boo - ShipControls.boo

This is a direct, verbatim conversion from the C# version. No attempt has been made to use Boo features. <boo> import UnityEngine import System.Collections

class ShipControls (MonoBehaviour): hoverHeight = 3F hoverHeightStrictness = 1F forwardThrust = 5000F backwardThrust = 2500F bankAmount = 0.1F bankSpeed = 0.2F bankAxis = Vector3(-1F, 0F, 0F) turnSpeed = 8000F

forwardDirection = Vector3(1F, 0F, 0F)

mass = 5F

// positional drag sqrdSpeedThresholdForDrag = 25F superDrag = 2F fastDrag = 0.5F slowDrag = 0.01F

// angular drag sqrdAngularSpeedThresholdForDrag = 5F superADrag = 32F fastADrag = 16F slowADrag = 0.1F

playerControl = false

bank = 0F

def SetPlayerControl(control as bool): playerControl = control

def Start(): rigidbody.mass = mass

def FixedUpdate(): if Mathf.Abs(thrust) > 0.01: if rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag: rigidbody.drag = fastDrag else: rigidbody.drag = slowDrag else: rigidbody.drag = superDrag

if Mathf.Abs(turn) > 0.01: if rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag: rigidbody.angularDrag = fastADrag else: rigidbody.angularDrag = slowADrag else: rigidbody.angularDrag = superADrag

transform.position = Vector3.Lerp(transform.position, Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness)

amountToBank as single = rigidbody.angularVelocity.y * bankAmount

bank = Mathf.Lerp(bank, amountToBank, bankSpeed)

rotation = transform.rotation.ToEulerAngles() rotation.x = 0F rotation.z = 0F rotation += bankAxis * bank transform.rotation = Quaternion.EulerAngles(rotation)

thrust = 0F turn = 0F

def Thrust(t as single): turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed

def Turn(t as single): turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed

thrustGlowOn = false

def Update (): theThrust = thrust

if (playerControl): thrust = Input.GetAxis("Thrust") turn = Input.GetAxis("Turn") * turnSpeed

if (thrust > 0.0): theThrust *= forwardThrust if (not thrustGlowOn): thrustGlowOn = not thrustGlowOn BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver) else: theThrust *= backwardThrust if (thrustGlowOn): thrustGlowOn = not thrustGlowOn BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver)

rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime) rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime) </boo>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox