ShipControls

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Boo - ShipControls.boo)
Line 13: Line 13:
 
== C# - ShipControls.cs ==
 
== C# - ShipControls.cs ==
 
<csharp>
 
<csharp>
using UnityEngine;
+
using UnityEngine;
using System.Collections;
+
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
+
// Put this on a rigidbody object and instantly
{
+
// have 2D spaceship controls like OverWhelmed Arena
public float hoverHeight = 3F;
+
// that you can tweak to your heart's content.
public float hoverHeightStrictness = 1F;
+
 
public float forwardThrust = 5000F;
+
 
public float backwardThrust = 2500F;
+
public class ShipControls : MonoBehaviour
public float bankAmount = 0.1F;
+
{
public float bankSpeed = 0.2F;
+
    public float hoverHeight = 3F;
public Vector3 bankAxis = new Vector3(-1F, 0F, 0F);
+
    public float hoverHeightStrictness = 1F;
public float turnSpeed = 8000F;
+
    public float forwardThrust = 5000F;
+
    public float backwardThrust = 2500F;
public Vector3 forwardDirection = new Vector3(1F, 0F, 0F);
+
    public float bankAmount = 0.1F;
+
    public float bankSpeed = 0.2F;
public float mass = 5F;
+
    public Vector3 bankAxis = new Vector3(-1F, 0F, 0F);
+
    public float turnSpeed = 8000F;
// positional drag
+
   
public float sqrdSpeedThresholdForDrag = 25F;
+
    public Vector3 forwardDirection = new Vector3(1F, 0F, 0F);
public float superDrag = 2F;
+
 
public float fastDrag = 0.5F;
+
    public float mass = 5F;
public float slowDrag = 0.01F;
+
   
+
    // positional drag
// angular drag
+
    public float sqrdSpeedThresholdForDrag = 25F;
public float sqrdAngularSpeedThresholdForDrag = 5F;
+
    public float superDrag = 2F;
public float superADrag = 32F;
+
    public float fastDrag = 0.5F;
public float fastADrag = 16F;
+
    public float slowDrag = 0.01F;
public float slowADrag = 0.1F;
+
   
+
    // angular drag
public bool playerControl = false;
+
    public float sqrdAngularSpeedThresholdForDrag = 5F;
+
    public float superADrag = 32F;
float bank = 0F;
+
    public float fastADrag = 16F;
+
    public float slowADrag = 0.1F;
void SetPlayerControl(bool control)
+
   
{
+
    public bool playerControl = false;
playerControl = control;
+
   
}
+
    float bank = 0F;
+
   
void Start()
+
    void SetPlayerControl(bool control)
{
+
    {
rigidbody.mass = mass;
+
        playerControl = control;
}
+
    }
+
 
void FixedUpdate()
+
    void Start()
{
+
    {
if (Mathf.Abs(thrust) > 0.01F)
+
        rigidbody.mass = mass;
{
+
    }
if (rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag)
+
   
rigidbody.drag = fastDrag;
+
    void FixedUpdate()
else
+
    {
rigidbody.drag = slowDrag;
+
        if (Mathf.Abs(thrust) > 0.01F)
}
+
        {
else
+
            if (rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag)
rigidbody.drag = superDrag;
+
                rigidbody.drag = fastDrag;
+
            else
if (Mathf.Abs(turn) > 0.01F)
+
                rigidbody.drag = slowDrag;
{
+
        }
if (rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag)
+
        else
rigidbody.angularDrag = fastADrag;
+
            rigidbody.drag = superDrag;
else
+
                   
rigidbody.angularDrag = slowADrag;
+
        if (Mathf.Abs(turn) > 0.01F)
}
+
        {
else
+
            if (rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag)
rigidbody.angularDrag = superADrag;
+
                rigidbody.angularDrag = fastADrag;
+
            else
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness);
+
                rigidbody.angularDrag = slowADrag;
+
        }
float amountToBank = rigidbody.angularVelocity.y * bankAmount;
+
        else
+
            rigidbody.angularDrag = superADrag;
bank = Mathf.Lerp(bank, amountToBank, bankSpeed);
+
       
+
        transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness);
Vector3 rotation = transform.rotation.ToEulerAngles();
+
       
rotation.x = 0F;
+
        float amountToBank = rigidbody.angularVelocity.y * bankAmount;
rotation.z = 0F;
+
 
rotation += bankAxis * bank;
+
        bank = Mathf.Lerp(bank, amountToBank, bankSpeed);
transform.rotation = Quaternion.EulerAngles(rotation);
+
       
}
+
        Vector3 rotation = transform.rotation.ToEulerAngles();
+
        rotation.x = 0F;
float thrust = 0F;
+
        rotation.z = 0F;
float turn = 0F;
+
        rotation += bankAxis * bank;
+
        transform.rotation = Quaternion.EulerAngles(rotation);
void Thrust(float t)
+
    }
{
+
   
thrust = Mathf.Clamp(t, -1F, 1F);
+
    float thrust = 0F;
}
+
    float turn = 0F;
+
   
void Turn(float t)
+
    void Thrust(float t)
{
+
    {
turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed;
+
        thrust = Mathf.Clamp(t, -1F, 1F);
}
+
    }
+
   
bool thrustGlowOn = false;
+
    void Turn(float t)
+
    {
void Update ()
+
        turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed;
{
+
    }
float theThrust = thrust;
+
   
+
    bool thrustGlowOn = false;
if (playerControl)
+
   
{
+
    void Update ()
thrust = Input.GetAxis("Thrust");
+
    {
turn = Input.GetAxis("Turn") * turnSpeed;
+
        float theThrust = thrust;
}
+
       
+
        if (playerControl)
if (thrust > 0F)
+
        {
{
+
            thrust = Input.GetAxis("Thrust");
theThrust *= forwardThrust;
+
            turn = Input.GetAxis("Turn") * turnSpeed;
if (!thrustGlowOn)
+
        }
{
+
               
thrustGlowOn = !thrustGlowOn;
+
        if (thrust > 0F)
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
+
        {
}
+
            theThrust *= forwardThrust;
}
+
            if (!thrustGlowOn)
else
+
            {
{
+
                thrustGlowOn = !thrustGlowOn;
theThrust *= backwardThrust;
+
                BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
if (thrustGlowOn)
+
            }
{
+
        }
thrustGlowOn = !thrustGlowOn;
+
        else
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
+
        {
}
+
            theThrust *= backwardThrust;
}
+
            if (thrustGlowOn)
+
            {
rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime);
+
                thrustGlowOn = !thrustGlowOn;
rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime);
+
                BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
}
+
            }
}
+
        }
 +
       
 +
        rigidbody.AddRelativeTorque(Vector3.up * turn * Time.deltaTime);
 +
        rigidbody.AddRelativeForce(forwardDirection * theThrust * Time.deltaTime);
 +
    }
 +
}
 
</csharp>
 
</csharp>
  
Line 154: Line 154:
  
 
class ShipControls (MonoBehaviour):
 
class ShipControls (MonoBehaviour):
hoverHeight = 3.0
+
    hoverHeight = 3.0
hoverHeightStrictness = 1.0
+
    hoverHeightStrictness = 1.0
forwardThrust = 5000.0
+
    forwardThrust = 5000.0
backwardThrust = 2500.0
+
    backwardThrust = 2500.0
bankAmount = 0.1
+
    bankAmount = 0.1
bankSpeed = 0.2
+
    bankSpeed = 0.2
bankAxis = Vector3(-1.0, 0.0, 0.0)
+
    bankAxis = Vector3(-1.0, 0.0, 0.0)
turnSpeed = 8000.0
+
    turnSpeed = 8000.0
+
   
forwardDirection = Vector3(1.0, 0.0, 0.0)
+
    forwardDirection = Vector3(1.0, 0.0, 0.0)
  
mass = 5.0
+
    mass = 5.0
+
   
// positional drag
+
    // positional drag
sqrdSpeedThresholdForDrag = 25.0
+
    sqrdSpeedThresholdForDrag = 25.0
superDrag = 2.0
+
    superDrag = 2.0
fastDrag = 0.5
+
    fastDrag = 0.5
slowDrag = 0.01
+
    slowDrag = 0.01
+
   
// angular drag
+
    // angular drag
sqrdAngularSpeedThresholdForDrag = 5.0
+
    sqrdAngularSpeedThresholdForDrag = 5.0
superADrag = 32.0
+
    superADrag = 32.0
fastADrag = 16.0
+
    fastADrag = 16.0
slowADrag = 0.1
+
    slowADrag = 0.1
  
playerControl = false
+
    playerControl = false
+
   
bank = 0.0
+
    bank = 0.0
+
   
def SetPlayerControl(control as bool):
+
    def SetPlayerControl(control as bool):
playerControl = control
+
        playerControl = control
  
def Start():
+
    def Start():
rigidbody.mass = mass
+
        rigidbody.mass = mass
+
   
def FixedUpdate():
+
    def FixedUpdate():
if Mathf.Abs(thrust) > 0.01:
+
        if Mathf.Abs(thrust) > 0.01:
if rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag:
+
            if rigidbody.velocity.sqrMagnitude > sqrdSpeedThresholdForDrag:
rigidbody.drag = fastDrag
+
                rigidbody.drag = fastDrag
else:
+
            else:
rigidbody.drag = slowDrag
+
                rigidbody.drag = slowDrag
else:
+
        else:
rigidbody.drag = superDrag
+
            rigidbody.drag = superDrag
  
if Mathf.Abs(turn) > 0.01:
+
        if Mathf.Abs(turn) > 0.01:
if rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag:
+
            if rigidbody.angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag:
rigidbody.angularDrag = fastADrag
+
                rigidbody.angularDrag = fastADrag
else:
+
            else:
rigidbody.angularDrag = slowADrag
+
                rigidbody.angularDrag = slowADrag
else:
+
        else:
rigidbody.angularDrag = superADrag
+
            rigidbody.angularDrag = superADrag
+
       
transform.position = Vector3.Lerp(transform.position, Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness)
+
        transform.position = Vector3.Lerp(transform.position, Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness)
  
amountToBank as single = rigidbody.angularVelocity.y * bankAmount
+
        amountToBank as single = rigidbody.angularVelocity.y * bankAmount
  
bank = Mathf.Lerp(bank, amountToBank, bankSpeed)
+
        bank = Mathf.Lerp(bank, amountToBank, bankSpeed)
+
       
rotation = transform.rotation.ToEulerAngles()
+
        rotation = transform.rotation.ToEulerAngles()
rotation.x = 0.0
+
        rotation.x = 0.0
rotation.z = 0.0
+
        rotation.z = 0.0
rotation += bankAxis * bank
+
        rotation += bankAxis * bank
transform.rotation = Quaternion.EulerAngles(rotation)
+
        transform.rotation = Quaternion.EulerAngles(rotation)
  
thrust = 0.0
+
    thrust = 0.0
turn = 0.0
+
    turn = 0.0
  
def Thrust(t as single):
+
    def Thrust(t as single):
turn = Mathf.Clamp(t, -1.0F, 1.0F) * turnSpeed
+
        turn = Mathf.Clamp(t, -1.0F, 1.0F) * turnSpeed
+
       
def Turn(t as single):
+
    def Turn(t as single):
turn = Mathf.Clamp(t, -1.0F, 1.0F) * turnSpeed
+
        turn = Mathf.Clamp(t, -1.0F, 1.0F) * turnSpeed
  
thrustGlowOn = false
+
    thrustGlowOn = false
+
   
def Update ():
+
    def Update ():
theThrust = thrust
+
        theThrust = thrust
  
if (playerControl):
+
        if (playerControl):
thrust = Input.GetAxis("Thrust")
+
            thrust = Input.GetAxis("Thrust")
turn = Input.GetAxis("Turn") * turnSpeed
+
            turn = Input.GetAxis("Turn") * turnSpeed
  
if (thrust > 0.0):
+
        if (thrust > 0.0):
theThrust *= forwardThrust
+
            theThrust *= forwardThrust
if (not thrustGlowOn):
+
            if (not thrustGlowOn):
thrustGlowOn = not thrustGlowOn
+
                thrustGlowOn = not thrustGlowOn
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver)
+
                BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver)
else:
+
        else:
theThrust *= backwardThrust
+
            theThrust *= backwardThrust
if (thrustGlowOn):
+
            if (thrustGlowOn):
thrustGlowOn = not thrustGlowOn
+
                thrustGlowOn = not thrustGlowOn
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver)
+
                BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver)
  
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>
 
</python>

Revision as of 08:28, 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. <python> import UnityEngine import System.Collections

class ShipControls (MonoBehaviour):

   hoverHeight = 3.0
   hoverHeightStrictness = 1.0
   forwardThrust = 5000.0
   backwardThrust = 2500.0
   bankAmount = 0.1
   bankSpeed = 0.2
   bankAxis = Vector3(-1.0, 0.0, 0.0)
   turnSpeed = 8000.0
   
   forwardDirection = Vector3(1.0, 0.0, 0.0)
   mass = 5.0
   
   // positional drag
   sqrdSpeedThresholdForDrag = 25.0
   superDrag = 2.0
   fastDrag = 0.5
   slowDrag = 0.01
   
   // angular drag
   sqrdAngularSpeedThresholdForDrag = 5.0
   superADrag = 32.0
   fastADrag = 16.0
   slowADrag = 0.1
   playerControl = false
   
   bank = 0.0
   
   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 = 0.0
       rotation.z = 0.0
       rotation += bankAxis * bank
       transform.rotation = Quaternion.EulerAngles(rotation)
   thrust = 0.0
   turn = 0.0
   def Thrust(t as single):
       turn = Mathf.Clamp(t, -1.0F, 1.0F) * turnSpeed
       
   def Turn(t as single):
       turn = Mathf.Clamp(t, -1.0F, 1.0F) * 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)

</python>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox