4DIS - Character Controllers

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== '''Character Controllers''' ==
 
== '''Character Controllers''' ==
 +
 +
[[4D IS]] - Back
  
 
----
 
----
Line 5: Line 7:
 
'''Roll a ball :'''
 
'''Roll a ball :'''
  
<javascript>
+
<syntaxhighlight lang="javascript">
  
 
@script RequireComponent ( Rigidbody )
 
@script RequireComponent ( Rigidbody )
Line 11: Line 13:
 
public var speed : float = 5.0;
 
public var speed : float = 5.0;
 
public var force : float = 7.5;
 
public var force : float = 7.5;
 +
 +
public var input : RollABallInput = new RollABallInput ( );
  
 
private var motion : Vector3 = new Vector3 ( 0, 0, 0 );
 
private var motion : Vector3 = new Vector3 ( 0, 0, 0 );
Line 16: Line 20:
 
function Update ( )
 
function Update ( )
 
{
 
{
 +
    if ( Input.GetKey ( input.buttonUp ) )
 +
        motion.z = speed;
 +
    else if ( Input.GetKey ( input.buttonDown ) )
 +
        motion.z = - speed;
 +
    else
 +
        motion.z = 0;
 +
   
 +
    if ( Input.GetKey ( input.buttonRight ) )
 +
        motion.x = speed;
 +
    else if ( Input.GetKey ( input.buttonLeft ) )
 +
        motion.x = - speed;
 +
    else
 +
        motion.x = 0;
 +
   
 +
    motion.y = rigidbody.velocity.y;
 +
   
 +
    rigidbody.velocity = Vector3.Lerp ( rigidbody.velocity, motion, Time.deltaTime * force );
 +
}
  
 +
class RollABallInput
 +
{
 +
    function RollABallInput ( ) { }
 +
   
 +
    public var buttonUp : String = "";
 +
    public var buttonLeft : String = "";
 +
    public var buttonDown : String = "";
 +
    public var buttonRight : String = "";
 +
}
  
</javascript>
+
</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

[edit] Character Controllers

4D IS - Back


Roll a ball :

@script RequireComponent ( Rigidbody )
 
public var speed : float = 5.0;
public var force : float = 7.5;
 
public var input : RollABallInput = new RollABallInput ( );
 
private var motion : Vector3 = new Vector3 ( 0, 0, 0 );
 
function Update ( )
{
    if ( Input.GetKey ( input.buttonUp ) )
        motion.z = speed;
    else if ( Input.GetKey ( input.buttonDown ) )
        motion.z = - speed;
    else
        motion.z = 0;
 
    if ( Input.GetKey ( input.buttonRight ) )
        motion.x = speed;
    else if ( Input.GetKey ( input.buttonLeft ) )
        motion.x = - speed;
    else
        motion.x = 0;
 
    motion.y = rigidbody.velocity.y;
 
    rigidbody.velocity = Vector3.Lerp ( rigidbody.velocity, motion, Time.deltaTime * force );
}
 
class RollABallInput
{
    function RollABallInput ( ) { }
 
    public var buttonUp : String = "";
    public var buttonLeft : String = "";
    public var buttonDown : String = "";
    public var buttonRight : String = "";
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox