4DIS - Camera Controller

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
'''Camera Controller A.js'''
 
'''Camera Controller A.js'''
  
<javascript>
+
<syntaxhighlight lang="javascript">
  
 
// An easy camera controller that uses position and rotation...
 
// An easy camera controller that uses position and rotation...
Line 36: Line 36:
 
}
 
}
  
</javascript>
+
</syntaxhighlight>
  
 
'''Camera Controller B.js'''
 
'''Camera Controller B.js'''
  
<javascript>
+
<syntaxhighlight lang="javascript">
  
// A camera controller follows the target while it´s looking on it...
+
// A camera controller that follows the target while it´s looking on it...
  
 
public static var target : Transform;
 
public static var target : Transform;
Line 75: Line 75:
 
}
 
}
  
</javascript>
+
</syntaxhighlight>
  
 
'''Camera Controller C.js'''
 
'''Camera Controller C.js'''
  
<javascript>
+
<syntaxhighlight lang="javascript">
  
 
// The same as "Camera Controller B" and this camera controller zooms back if the target is moving with a "Rigidbody"-component...
 
// The same as "Camera Controller B" and this camera controller zooms back if the target is moving with a "Rigidbody"-component...
Line 133: Line 133:
 
}
 
}
  
</javascript>
+
</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

[edit] Camera Controllers

4D IS - Back


Camera Controller A.js

// An easy camera controller that uses position and rotation...
 
public var target : Transform;
 
public var positionOffset : Vector3 = new Vector3 ( 0, 0, 0 );
public var rotationOffset : Vector3 = new Vector3 ( 0, 0, 0 );
public var positionForce : float = 5.0;
public var rotationForce : float = 5.0;
 
private var position : Vector3 = new Vector3 ( 0, 0, 0 );
private var rotation : Quaternion = new Quaternion ( 0, 0, 0, 0 );
 
function Update ( )
{
	if ( ! target )
		return null;
 
	position = Vector3.Lerp ( position, target.position, Time.deltaTime * positionForce );
	rotation = Quaternion.Lerp ( rotation, target.rotation, Time.deltaTime * rotationForce );
 
	transform.position = position;
	transform.rotation = rotation;
 
	transform.Rotate ( rotationOffset, Space.Self );
	transform.Translate ( positionOffset, Space.Self );
}

Camera Controller B.js

// A camera controller that follows the target while it´s looking on it...
 
public static var target : Transform;
 
public var positionOffset : Vector3 = new Vector3 ( 0, 1, 4 );
public var positionForce : float = 5.0;
public var lookFactor : float = 0.3;
public var lookForce : float = 0.6;
 
private var position : Vector3 = new Vector3 ( 0, 0, 0 );
private var rotation : Vector3 = new Vector3 ( 0, 0, 0 );
 
function Update ( )
{
	if ( ! target )
		return null;
 
	if ( positionForce > 0 )
		position = Vector3.Lerp ( position, target.position, Time.deltaTime * positionForce );
	else
		position = Vector3 ( 0, 0, 0 );
 
	if ( target.rigidbody )
		rotation = Vector3.Lerp ( rotation, target.rigidbody.velocity * lookFactor, Time.deltaTime * lookForce );
 
	transform.rotation = Quaternion ( 0, 0, 0, 0 );
 
	transform.position = position;
	transform.Translate ( positionOffset, Space.Self );
 
	transform.LookAt ( target.position + rotation );
}

Camera Controller C.js

// The same as "Camera Controller B" and this camera controller zooms back if the target is moving with a "Rigidbody"-component...
 
public static var target : Transform;
 
public var positionOffset : Vector3 = new Vector3 ( 0, 1.5, -4 );
public var positionForce : float = 5.0;
public var lookFactor : float = 0.3;
public var lookForce : float = 0.6;
public var viewForce : float = 5.0;
public var minimalView : float = 60.0;
public var maximalView : float = 140.0;
public var maximalViewSpeed : float = 40.0;
 
private var position : Vector3 = new Vector3 ( 0, 0, 0 );
private var rotation : Vector3 = new Vector3 ( 0, 0, 0 );
private var view : float = 0.0;
 
function Awake ( )
{
	view = minimalView;
}
 
function FixedUpdate ( )
{
	if ( ! target )
		return null;
 
	if ( positionForce > 0 )
		position = Vector3.Lerp ( position, target.position, Time.deltaTime * positionForce );
	else
		position = Vector3 ( 0, 0, 0 );
 
	if ( target.rigidbody )
	{
		rotation = Vector3.Lerp ( rotation, target.rigidbody.velocity * lookFactor, Time.deltaTime * lookForce );
 
		view = Mathf.Lerp ( view, Mathf.Lerp ( minimalView, maximalView, target.rigidbody.velocity.magnitude / maximalViewSpeed ), Time.deltaTime * viewForce );
 
		if ( camera )
		{
			camera.fieldOfView = view;
		}
	}	
 
	transform.rotation = Quaternion ( 0, 0, 0, 0 );
 
	transform.position = position;
	transform.Translate ( positionOffset, Space.Self );
 
	transform.LookAt ( target.position + rotation );
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox