Force2D

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - Force2D.js)
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
(24 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 +
[[Category: Two Dimensional]]
 +
[[Category: MonoBehaviour]]
 +
[[Category: JavaScript]]
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
 
==Description==
 
==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==
 
==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.
 +
 +
EDIT: the JS can now be made to constrain to x y or z, and will do so there original non constrained rotations
 +
 
==JavaScript - Force2D.js==
 
==JavaScript - Force2D.js==
var depth = 0.0;
+
 
+
<syntaxhighlight lang="javascript">
  function Awake()
+
 
{
+
//By Nik Gabo
depth = transform.position.x;
+
//now its modular ;)
}
+
public var forceBetweenXYZ:String="x";
+
private var Axis:Vector3;//desired depth
function FixedUpdate ()
+
private var Rotation:Vector3;//desired rotation
{
+
 
transform.position.x = depth;
+
function Awake(){
fwd = transform.localRotation * Vector3.fwd;
+
    Axis=transform.position;//defines the starting X position as the desired X position
fwd.x = 0;
+
Rotation=transform.eulerAngles;//defines the starting rotation as the desired rotation
transform.localRotation = Quaternion.LookRotation (fwd, -Vector3.Cross (Vector3.right, fwd));
+
}
}
+
  function Update(){//each frame
 +
var spinforce:Vector3=gameObject.rigidbody.angularVelocity;//is the rotational force on this frame
 +
var place:Vector3=gameObject.transform.position;//is the target position in world space on this frame
 +
 +
if(forceBetweenXYZ=="x"){
 +
if(place.x!=Axis.x){//if the current X position IS NOT the desired X position then...
 +
transform.position.x=Axis.x;//go to the desired X position
 +
}
 +
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
 +
gameObject.rigidbody.angularVelocity.z=0;//stop rotating around z axis
 +
gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
 +
gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
 +
}else if(forceBetweenXYZ=="y"){
 +
if(place.y!=Axis.y){//if the current X position IS NOT the desired X position then...
 +
transform.position.y=Axis.y;//go to the desired X position
 +
}
 +
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
 +
gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
 +
gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
 +
gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
 +
}else{
 +
if(place.z!=Axis.z){//if the current X position IS NOT the desired X position then...
 +
transform.position.z=Axis.z;//go to the desired X position
 +
}
 +
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
 +
gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
 +
gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
 +
gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
 +
if(forceBetweenXYZ!="z"){
 +
print("FORCING Z BY DEFAULT, USE X, Y, OR Z LOWERCASE");
 +
}
 +
}
 +
}
 +
 
 +
 
 +
</syntaxhighlight>
 +
 
 +
==CShapr - Force2D.cs==
 +
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
 
 +
public class Force2D:MonoBehaviour{
 +
private Transform trans;
 +
private Rigidbody body;
 +
//historical x-position
 +
private float x;
 +
 +
public void Start ()
 +
{
 +
//cashe components for good performance
 +
trans = transform;
 +
body = rigidbody;
 +
 +
x = trans.position.x;
 +
}
 +
 +
public void FixedUpdate ()
 +
{
 +
//reset x-position
 +
Vector3 pos = trans.position;
 +
pos.x = x;
 +
trans.position = pos;
 +
 +
//clear Y and Z rotation
 +
Vector3 dir = trans.localRotation * Vector3.forward;
 +
dir.x = 0;
 +
trans.localRotation = Quaternion.LookRotation (dir, -Vector3.Cross (Vector3.right, dir));
 +
 +
//clear X-velocity
 +
Vector3 vel = body.velocity;
 +
vel.x = 0;
 +
body.velocity = vel;
 +
 +
//clear Y and Z angular velocity
 +
body.angularVelocity = new Vector3(body.angularVelocity.x, 0, 0);
 +
}
 +
}</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

Author: Jonathan Czeck (aarku)

Contents

[edit] 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.

[edit] 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.

EDIT: the JS can now be made to constrain to x y or z, and will do so there original non constrained rotations

[edit] JavaScript - Force2D.js

//By Nik Gabo
//now its modular ;)
public var forceBetweenXYZ:String="x";
private var Axis:Vector3;//desired depth
private var Rotation:Vector3;//desired rotation
 
function Awake(){
    Axis=transform.position;//defines the starting X position as the desired X position
	Rotation=transform.eulerAngles;//defines the starting rotation as the desired rotation
}
 function Update(){//each frame
	var spinforce:Vector3=gameObject.rigidbody.angularVelocity;//is the rotational force on this frame
	var place:Vector3=gameObject.transform.position;//is the target position in world space on this frame
 
	if(forceBetweenXYZ=="x"){
		if(place.x!=Axis.x){//if the current X position IS NOT the desired X position then...
			transform.position.x=Axis.x;//go to the desired X position
		}
		gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
		gameObject.rigidbody.angularVelocity.z=0;//stop rotating around z axis
		gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
		gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
	}else if(forceBetweenXYZ=="y"){
		if(place.y!=Axis.y){//if the current X position IS NOT the desired X position then...
			transform.position.y=Axis.y;//go to the desired X position
		}
		gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
		gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
		gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
		gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
	}else{
		if(place.z!=Axis.z){//if the current X position IS NOT the desired X position then...
			transform.position.z=Axis.z;//go to the desired X position
		}
		gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
		gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
		gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
		gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
		if(forceBetweenXYZ!="z"){
			print("FORCING Z BY DEFAULT, USE X, Y, OR Z LOWERCASE");
		}
	}
}

[edit] CShapr - Force2D.cs

using UnityEngine;
 
public class Force2D:MonoBehaviour{
	private Transform trans;
	private Rigidbody body;
	//historical x-position
	private float x;
 
	public void Start ()
	{
		//cashe components for good performance
		trans = transform;
		body = rigidbody;
 
		x = trans.position.x;
	}
 
	public void FixedUpdate ()
	{
		//reset x-position
		Vector3 pos = trans.position;
		pos.x = x;
		trans.position = pos;
 
		//clear Y and Z rotation
		Vector3 dir = trans.localRotation * Vector3.forward;
		dir.x = 0;
		trans.localRotation = Quaternion.LookRotation (dir, -Vector3.Cross (Vector3.right, dir));
 
		//clear X-velocity
		Vector3 vel = body.velocity;
		vel.x = 0;
		body.velocity = vel;
 
		//clear Y and Z angular velocity
		body.angularVelocity = new Vector3(body.angularVelocity.x, 0, 0);
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox