Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Personaje : MonoBehaviour {
- private Rigidbody2D myRigidBody;
- private Animator myAnimator;
- [SerializeField] //para poder editarlo en el inspector
- private float movementSpeed;
- private bool attack;
- private bool direccionPersonaje;
- [SerializeField]
- private Transform[] groundPoints;
- [SerializeField]
- private float groundRadius;
- [SerializeField]
- private LayerMask whatIsGround;
- private bool isGrounded;
- private bool jump;
- [SerializeField]
- private float jumpForce;
- public GameObject leftBullet, rightBullet;
- Transform firePos;
- // Use this for initialization
- void Start () {
- direccionPersonaje = true;
- //Referenciamos el rigidbody que tenemos en el player con la variable que hemos creado arriba
- myRigidBody = GetComponent<Rigidbody2D>();
- myAnimator = GetComponent<Animator>();
- GetComponent<Rigidbody2D>().angularVelocity = 0;
- firePos = transform.FindChild ("firePos");
- }
- void Update()
- {
- HandleInput();
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- float horizontal = Input.GetAxis ("Horizontal");
- isGrounded = IsGrounded ();
- HandleMovement (horizontal);
- Flip (horizontal);
- if (Input.GetKeyDown (KeyCode.K))
- {
- Fire ();
- }
- HandleAttacks();
- ResetValues();
- }
- private void HandleMovement(float horizontal)
- {
- if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag ("Attack"))
- {
- myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
- }
- if (isGrounded && jump) {
- isGrounded = false;
- myRigidBody.AddForce (new Vector2 (0, jumpForce));
- myAnimator.SetTrigger ("Jump");
- }
- myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
- }
- private void HandleAttacks()
- {
- if (attack)
- {
- myAnimator.SetTrigger("attack");
- myRigidBody.velocity = Vector2.zero;
- }
- }
- private void HandleInput()
- {
- if (Input.GetKeyDown (KeyCode.K))
- {
- attack = true;
- }
- if (Input.GetKeyDown (KeyCode.Space)) {
- jump = true;
- }
- }
- private void Flip(float horizontal)
- {
- if (horizontal > 0 && !direccionPersonaje || horizontal < 0 && direccionPersonaje) {
- direccionPersonaje = !direccionPersonaje;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- }
- private void ResetValues()
- {
- attack = false;
- jump = false;
- }
- private bool IsGrounded (){
- if (myRigidBody.velocity.y <= 0) { //si estamos parados en el eje y ( en el suelo)
- foreach (Transform point in groundPoints) {
- Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, groundRadius, whatIsGround);
- for (int i = 0; i < colliders.Length; i++) {
- if (colliders [i].gameObject != gameObject) {
- myAnimator.ResetTrigger ("Jump");
- return true;
- }
- }
- }
- }
- return false;
- }
- void Fire()
- {
- if (direccionPersonaje)
- {
- Instantiate(rightBullet, firePos.position, Quaternion.identity);
- }
- if (!direccionPersonaje)
- {
- Instantiate(leftBullet, firePos.position, Quaternion.identity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement