Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerScript : MonoBehaviour {
- [HideInInspector] public bool facingRight = true;
- [HideInInspector] public bool jump = false;
- public float moveForce = 265f;
- public float maxSpeed = 5f;
- public float jumpForce = 1000f;
- public Transform groundCheck;
- public GameObject heroShadow;
- public AudioSource audioSource;
- public AudioClip footStepSound;
- private bool grounded = false;
- private Animator anim;
- private Rigidbody2D rb2d;
- // Use this for initialization
- void Awake ()
- {
- anim = GetComponent<Animator>();
- rb2d = GetComponent<Rigidbody2D>();
- audioSource = GetComponent<AudioSource> ();
- }
- // Update is called once per frame
- void Update ()
- {
- grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
- if (Input.GetButtonDown ("Jump") && grounded) {
- jump = true;
- anim.SetBool ("isJumping", true);
- }
- if (grounded == false) {
- heroShadow.gameObject.SetActive (false);
- anim.SetBool ("isJumping", true);
- anim.SetBool ("isWalking", false);
- anim.SetBool ("isIdle", false);
- } else {
- heroShadow.gameObject.SetActive (true);
- anim.SetBool ("isJumping", false);
- grounded = true;
- }
- }
- void FixedUpdate()
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis ("Vertical");
- /*
- if (v != 0f) {
- anim.SetBool ("isJumping", true);
- } else {
- anim.SetBool ("isJumping", false);
- }
- */
- if (h != 0f && jump == true && grounded == false) {
- anim.SetBool ("isJumping", true);
- anim.SetBool ("isWalking", false);
- anim.SetBool ("isIdle", false);
- }
- else if (h != 0f && jump == false && grounded) {
- anim.SetBool ("isWalking", true);
- anim.SetBool ("isJumping", false);
- anim.SetBool ("isIdle", false);
- audioSource.PlayOneShot(footStepSound);
- } else if (h <= 1f && jump == false && grounded) {
- anim.SetBool ("isIdle", true);
- anim.SetBool ("isWalking", false);
- anim.SetBool ("isJumping", false);
- } else if (h <= 1f && jump == true && grounded == false) {
- anim.SetBool ("isJumping", true);
- anim.SetBool ("isIdle", false);
- anim.SetBool ("isWalking", false);
- } else if (v != 0f && grounded == false) {
- anim.SetBool ("isJumping", true);
- anim.SetBool ("isIdle", false);
- anim.SetBool ("isWalking", false);
- }
- if (v != 0f && h != 0f && jump == true && grounded == false) {
- anim.SetBool ("isJumping", true);
- anim.SetBool ("isWalking", false);
- anim.SetBool ("isIdle", false);
- }
- anim.SetFloat("Speed", Mathf.Abs(h));
- if (h * rb2d.velocity.x < maxSpeed)
- rb2d.AddForce (Vector2.right * h * moveForce);
- if (Mathf.Abs (rb2d.velocity.x) > maxSpeed) {
- rb2d.velocity = new Vector2 (Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
- }
- if (h < 0 && !facingRight)
- Flip ();
- else if (h > 0 && facingRight)
- Flip ();
- if (jump) {
- //anim.SetTrigger("Jump");
- anim.SetBool ("isJumping", true);
- rb2d.AddForce (new Vector2 (0f, jumpForce));
- //StartCoroutine (JumpLanding ());
- jump = false;
- }
- }
- void Flip()
- {
- facingRight = !facingRight;
- Vector3 theScale = transform.localScale;
- theScale.x *= -1;
- transform.localScale = theScale;
- }
- /*IEnumerator JumpLanding() {
- yield return new WaitForSeconds (0.01f);
- anim.SetBool ("isJumping", false);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement