4DIS - Character Controllers

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 11: Line 11:
 
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 23: Line 25:
 
         motion.z = 0;
 
         motion.z = 0;
 
      
 
      
     if ( Input.GetKey ( input.buttonUp ) )
+
     if ( Input.GetKey ( input.buttonRight ) )
         motion.z = speed;
+
         motion.x = speed;
     else if ( Input.GetKey ( input.buttonDown ) )
+
     else if ( Input.GetKey ( input.buttonLeft ) )
         motion.z = - speed;
+
         motion.x = - speed;
 
     else
 
     else
         motion.z = 0;
+
         motion.x = 0;
 +
   
 +
    motion.y = rigidbody.velocity.y;
 
      
 
      
 
     rigidbody.velocity = Vector3.Lerp ( rigidbody.velocity, motion, Time.deltaTime * force );
 
     rigidbody.velocity = Vector3.Lerp ( rigidbody.velocity, motion, Time.deltaTime * force );
 
}
 
}
  
class  
+
class RollABallInput
 +
{
 +
    function RollABallInput ( ) { }
 +
   
 +
    public var buttonUp : String = "";
 +
    public var buttonLeft : String = "";
 +
    public var buttonDown : String = "";
 +
    public var buttonRight : String = "";
 +
}
  
 
</javascript>
 
</javascript>

Revision as of 16:04, 29 October 2011

Character Controllers


Roll a ball :

<javascript>

@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 = "";

}

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox