Skip to content

Instantly share code, notes, and snippets.

@nicloay
Created February 14, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicloay/9002437 to your computer and use it in GitHub Desktop.
Save nicloay/9002437 to your computer and use it in GitHub Desktop.
example controller which works with mecanim
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class KevinController : MonoBehaviour {
public float maxSpeed = 10f;
public float groundCheckRadius = 0.1f;
public LayerMask groundCechkLayerMask;
public float jumpForce = 100f;
public float runJumpForce = 200f;
public Vector2 longJumpForce = new Vector2(100,100);
public static int idleState = Animator.StringToHash("Base Layer.Idle");
public static int jumpLanding = Animator.StringToHash("Base Layer.jumpLanding");
public static int jumping = Animator.StringToHash("Base Layer.jumping");
float runSpeedRatio = 2f;
bool facedRight = false;
Animator animator;
bool isGrounded;
void Start(){
animator = GetComponent<Animator>();
}
AnimatorStateInfo currentState;
bool longJumpInProgress = false;
void Update(){
currentState = animator.GetCurrentAnimatorStateInfo(0);
float vertical = Input.GetAxis("Vertical");
if (isGrounded && Input.GetButtonDown("Jump")){
if (vertical < 0){
animator.SetTrigger("roll");
} else {
if (currentState.nameHash == idleState){
longJumpInProgress = true;
animator.SetTrigger("longJump");
} else {
isGrounded = false;
animator.SetBool("isGrounded", isGrounded);
if (Input.GetKey(KeyCode.LeftShift) )
rigidbody2D.AddForce(new Vector2(0,runJumpForce));
else
rigidbody2D.AddForce(new Vector2(0,jumpForce));
animator.CrossFade(jumping,0.1f,0,0.1f);
}
}
}
if (Input.GetButtonUp("Jump") && longJumpInProgress){
Vector2 force = facedRight ? longJumpForce : new Vector2( -longJumpForce.x, longJumpForce.y);
isGrounded = false;
animator.SetBool("isGrounded", isGrounded);
rigidbody2D.AddForce(force);
longJumpInProgress = false;
}
if (Input.GetButtonDown("Fire1"))
animator.SetTrigger("Fire1");
if (Input.GetButton("Fire3"))
animator.SetTrigger("Fire3");
animator.SetBool("block", Input.GetButton("Fire2"));
}
float moveX, moveXAbs;
AnimationState animationState;
void FixedUpdate () {
isGrounded = Physics2D.OverlapCircle(transform.position , groundCheckRadius, groundCechkLayerMask);
animator.SetBool("isGrounded", isGrounded);
moveX = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.LeftShift) )
moveX *= runSpeedRatio;
moveXAbs = Mathf.Abs(moveX);
animator.SetFloat("moveXAbs", moveXAbs);
if (isGrounded)
rigidbody2D.velocity = new Vector2(moveX * maxSpeed, rigidbody2D.velocity.y);
if ((moveX > 0 && !facedRight) || (moveX < 0 && facedRight))
flip();
animator.SetFloat("verticalSpeed", rigidbody2D.velocity.y);
}
void flip(){
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
facedRight = !facedRight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment