Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlatformerScript : MonoBehaviour {
  6.     public bool facingRight = true;
  7.     public SpriteRenderer spriteRenderer;
  8.     public Sprite JumpingOne;
  9.     public Sprite JumpingTwo;
  10.     public Sprite IdleSprite;
  11.     public Sprite RunningOne;
  12.     public Sprite RunningTwo;
  13.     public Sprite RunningThree;
  14.     public Sprite RunningFour;
  15.     public GameObject Player;
  16.     public float speed = 6.0F;
  17.     public float jumpSpeed = 8.0F;
  18.     public float gravity = 20.0F;
  19.     private Vector3 moveDirection = Vector3.zero;
  20.     public CharacterController controller;
  21.     public float LegMoveRate = 0.2f;
  22.     public float MoveHorizontal;
  23.     public bool Moving = false;
  24.     public bool IsJumping = false;
  25.     /// <summary>
  26.     ///
  27.     /// </summary>
  28.     void Update()
  29.     {
  30.         transform.position = new Vector3(transform.position.x, transform.position.y, -1);
  31.         float Xpos = transform.position.x;
  32.         float Ypos = transform.position.y;
  33.  
  34.         MoveHorizontal = Input.GetAxis("Horizontal");
  35.         if (controller.isGrounded == true)
  36.         {
  37.             IsJumping = false;
  38.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
  39.             moveDirection = transform.TransformDirection(moveDirection);
  40.             moveDirection *= speed;
  41.             if (Input.GetButton("Jump"))
  42.             {
  43.                 moveDirection.y = jumpSpeed;
  44.             }
  45.         }
  46.         if (IsJumping == true)
  47.         {
  48.             if (controller.velocity.y >= 0)
  49.             {
  50.                 spriteRenderer.sprite = JumpingOne;
  51.             }
  52.             if (controller.velocity.y < 0)
  53.             {
  54.                 spriteRenderer.sprite = JumpingTwo;
  55.             }
  56.         }
  57.         if (controller.isGrounded == false)
  58.         {
  59.             IsJumping = true;
  60.         }
  61.  
  62.         moveDirection.y -= gravity * Time.deltaTime;
  63.         controller.Move(moveDirection * Time.deltaTime);
  64.  
  65.         if (MoveHorizontal > 0 && !facingRight)
  66.         {
  67.             Flip();
  68.         }
  69.         else if (MoveHorizontal < 0 && facingRight)
  70.         {
  71.             Flip();
  72.         }
  73.         if (MoveHorizontal == 0)
  74.         {
  75.  
  76.             if (IsJumping == false)
  77.             {
  78.                 spriteRenderer.sprite = IdleSprite;
  79.             }
  80.             else if (IsJumping == true)
  81.             {
  82.                 if (controller.velocity.y >= 0)
  83.                 {
  84.                     spriteRenderer.sprite = JumpingOne;
  85.                 }
  86.                 if (controller.velocity.y < 0)
  87.                 {
  88.                     spriteRenderer.sprite = JumpingTwo;
  89.                 }
  90.             }
  91.  
  92.             Moving = false;
  93.             CancelInvoke("MovingLegs");
  94.             StopCoroutine("MovingLegs");
  95.         }
  96.         if (MoveHorizontal != 0 && IsJumping == false)
  97.         {
  98.             InvokeRepeating("MovingLegs", 0f, LegMoveRate * 4);
  99.         }
  100.     }
  101.     void MoveLegs ()
  102.     {
  103.         StartCoroutine("MovingLegs");
  104.     }
  105.     IEnumerator MovingLegs ()
  106.     {
  107.         yield return new WaitForSeconds(LegMoveRate);
  108.         spriteRenderer.sprite = RunningOne;
  109.         yield return new WaitForSeconds(LegMoveRate);
  110.         spriteRenderer.sprite = RunningTwo;
  111.         yield return new WaitForSeconds(LegMoveRate);
  112.         spriteRenderer.sprite = RunningThree;
  113.         yield return new WaitForSeconds(LegMoveRate);
  114.         spriteRenderer.sprite = RunningFour;
  115.     }
  116.     void Flip()
  117.     {
  118.         facingRight = !facingRight;
  119.         Player.transform.Rotate(0, 180, 0);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement