2013년 6월 16일 일요일

Vscroll.cs

using System.Collections;

public class Vscroll : MonoBehaviour {

public Joystick[] moveTouchPad;
public Joystick[] jumpTouchPad;


public float forwardSpeed = 4.0f;
public float backwardSpeed = 4.0f;
public float jumpSpeed = 16;
public float inAirMultiplier = 0.25f; // Limiter for ground speed while jumping

private Transform thisTransform;
private CharacterController character;
private Vector3 velocity; // Used for continuing momentum while in air
private bool canJump = true;

// Use this for initialization
void Start () {

// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent< Transform >();
character = GetComponent< CharacterController >();
//joystick = GetComponent< CharacterController >();


// Move the character to the correct start position in the level, if one exists
GameObject spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;

moveTouchPad = new Joystick();
}


void OnEndGame()
{
// Disable joystick when the game ends
moveTouchPad.Disable();
jumpTouchPad.Disable();

// Don't allow any more control changes when the game ends
this.enabled = false;
}



// Update is called once per frame
void Update()
{
Vector3 movement = Vector3.zero;

// Apply movement from move joystick
if ( moveTouchPad.position.x > 0 )
movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
else
movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;

// Check for jump
if ( character.isGrounded )
{
bool jump = false;
var touchPad = jumpTouchPad;

if ( !touchPad.IsFingerDown() )
canJump = true;

if ( canJump && touchPad.IsFingerDown() )
{
jump = true;
canJump = false;
}

if ( jump )
{
// Apply the current movement to launch velocity
velocity = character.velocity;
velocity.y = jumpSpeed;
}
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;

// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
// movement.z *= inAirMultiplier;
}

movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;

// Actually move the character
character.Move( movement );

if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
}
}

댓글 없음:

댓글 쓰기