Force2D

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Uvmarko)
Line 1: Line 1:
 +
[[Category: Two Dimensional]]
 +
[[Category: MonoBehaviour]]
 +
[[Category: JavaScript]]
 +
Author: Jonathan Czeck (aarku)
 +
==Description==
 +
This script will allow you to constrain Unity's physics engine to two dimensions.  It works best for rigidbodies that are not connected by physics joints.
  
 +
Technically, this script constrains the GameObject to the Y-Z plane with rotations only allowed on the X axis.
 +
 +
==Usage==
 +
Assign this behavior to any GameObject.  Do not assign this to RigidBodies that are linked together with joints if you want to keep the physics engine more stable.
 +
 +
* Note that using this script as is will prevent rigidbodies from sleeping . the workaround will be to check whether the object needs alignment before assigning a value to it.
 +
 +
==JavaScript - Force2D.js==
 +
<javascript>
 +
var depth = 0.0;
 +
 +
function Awake()
 +
{
 +
    depth = transform.position.x;
 +
}
 +
 +
function FixedUpdate ()
 +
{
 +
    transform.position.x = depth;
 +
    fwd = transform.localRotation * Vector3.fwd;
 +
    fwd.x = 0;
 +
    transform.localRotation = Quaternion.LookRotation (fwd, -Vector3.Cross (Vector3.right, fwd));
 +
}
 +
</javascript>

Revision as of 19:11, 19 October 2009

Author: Jonathan Czeck (aarku)

Description

This script will allow you to constrain Unity's physics engine to two dimensions. It works best for rigidbodies that are not connected by physics joints.

Technically, this script constrains the GameObject to the Y-Z plane with rotations only allowed on the X axis.

Usage

Assign this behavior to any GameObject. Do not assign this to RigidBodies that are linked together with joints if you want to keep the physics engine more stable.

  • Note that using this script as is will prevent rigidbodies from sleeping . the workaround will be to check whether the object needs alignment before assigning a value to it.

JavaScript - Force2D.js

<javascript>

var depth = 0.0;

function Awake()
{
   depth = transform.position.x;
}

function FixedUpdate ()
{
   transform.position.x = depth;
   fwd = transform.localRotation * Vector3.fwd;
   fwd.x = 0;
   transform.localRotation = Quaternion.LookRotation (fwd, -Vector3.Cross (Vector3.right, fwd));
}

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox