Game Development Reference
In-Depth Information
}
}
9. Then, we create a FixedUpdate() function, which will handle the animation
state changing ( Idle , Walk , Jump , or Fall ) and add force to move our character
horizontally and vertically. Let's add this function as follows:
// Unity JavaScript user:
function FixedUpdate () {
if (!_gameEnd) {
_animator.SetFloat("Speed",
Mathf.Abs(_horizontalInput));
var xSpeed : float = Mathf.Abs(_horizontalInput *
rigidbody2D.velocity.x);
if (xSpeed < maxSpeed) {
rigidbody2D.AddForce(Vector2.right *
_horizontalInput * moveForce);
}
if (Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
{
var newVelocity : Vector2 =
rigidbody2D.velocity;
newVelocity.x = Mathf.Sign(newVelocity.x) *
maxSpeed;
rigidbody2D.velocity = newVelocity;
}
if(_isJump) {
_animator.SetTrigger("Jump");
audio.volume = 0.3f;
audio.PlayOneShot(jumpSound);
rigidbody2D.AddForce(new Vector2(0f,
jumpForce));
_isJump = false;
}
if (!_isGrounded) {
if ((rigidbody2D.velocity.y <= 0f) && !_isFall)
{
_animator.SetTrigger("Fall");
Search WWH ::




Custom Search