Labo_2_equ_2_a15/Assets/Scripts/PlayerBehavior.cs
alsarazin 95dbd14e66 Merge remote-tracking branch 'origin/master'
Conflicts:
	Assets/Scripts/PlayerBehavior.cs
2015-11-09 17:12:51 -05:00

63 lines
1.6 KiB
C#

using UnityEngine;
using System.Collections;
public class PlayerBehavior : MonoBehaviour {
public float maxSpeed = 50.0f;
// Jump Stuff
public bool grounded = true;
public Transform groundCheck;
private float groundRadius = 0.1f;
public LayerMask ground;
public float jumpForce = 7.0f;
private Rigidbody2D rb;
private Animator anim;
private bool facingRight = true;
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(grounded && Input.GetAxis("Vertical") > 0.0f)
{
anim.SetBool("Ground", false);
rb.AddForce(new Vector2(0, jumpForce));
}
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, ground);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", rb.velocity.y);
float xMove = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(xMove * maxSpeed, rb.velocity.y);
if ((xMove < 0 && facingRight) || (xMove > 0 && !facingRight))
flip();
// Change animation according to user inputs
anim.SetFloat("Speed", Mathf.Abs(xMove));
}
public bool GetFacingRight()
{
return facingRight;
}
private void flip()
{
facingRight = !facingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}