Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlatformerScript : MonoBehaviour {
- public bool facingRight = true;
- public SpriteRenderer spriteRenderer;
- public Sprite JumpingOne;
- public Sprite JumpingTwo;
- public Sprite IdleSprite;
- public Sprite RunningOne;
- public Sprite RunningTwo;
- public Sprite RunningThree;
- public Sprite RunningFour;
- public GameObject Player;
- public float speed = 6.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- public CharacterController controller;
- public float LegMoveRate = 0.2f;
- public float MoveHorizontal;
- public bool Moving = false;
- public bool IsJumping = false;
- /// <summary>
- ///
- /// </summary>
- void Update()
- {
- transform.position = new Vector3(transform.position.x, transform.position.y, -1);
- float Xpos = transform.position.x;
- float Ypos = transform.position.y;
- MoveHorizontal = Input.GetAxis("Horizontal");
- if (controller.isGrounded == true)
- {
- IsJumping = false;
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- if (Input.GetButton("Jump"))
- {
- moveDirection.y = jumpSpeed;
- }
- }
- if (IsJumping == true)
- {
- if (controller.velocity.y >= 0)
- {
- spriteRenderer.sprite = JumpingOne;
- }
- if (controller.velocity.y < 0)
- {
- spriteRenderer.sprite = JumpingTwo;
- }
- }
- if (controller.isGrounded == false)
- {
- IsJumping = true;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- if (MoveHorizontal > 0 && !facingRight)
- {
- Flip();
- }
- else if (MoveHorizontal < 0 && facingRight)
- {
- Flip();
- }
- if (MoveHorizontal == 0)
- {
- if (IsJumping == false)
- {
- spriteRenderer.sprite = IdleSprite;
- }
- else if (IsJumping == true)
- {
- if (controller.velocity.y >= 0)
- {
- spriteRenderer.sprite = JumpingOne;
- }
- if (controller.velocity.y < 0)
- {
- spriteRenderer.sprite = JumpingTwo;
- }
- }
- Moving = false;
- CancelInvoke("MovingLegs");
- StopCoroutine("MovingLegs");
- }
- if (MoveHorizontal != 0 && IsJumping == false)
- {
- InvokeRepeating("MovingLegs", 0f, LegMoveRate * 4);
- }
- }
- void MoveLegs ()
- {
- StartCoroutine("MovingLegs");
- }
- IEnumerator MovingLegs ()
- {
- yield return new WaitForSeconds(LegMoveRate);
- spriteRenderer.sprite = RunningOne;
- yield return new WaitForSeconds(LegMoveRate);
- spriteRenderer.sprite = RunningTwo;
- yield return new WaitForSeconds(LegMoveRate);
- spriteRenderer.sprite = RunningThree;
- yield return new WaitForSeconds(LegMoveRate);
- spriteRenderer.sprite = RunningFour;
- }
- void Flip()
- {
- facingRight = !facingRight;
- Player.transform.Rotate(0, 180, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement