Advertisement
SizilStank

Untitled

Mar 12th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     [SerializeField] private float walkingspeed;
  8.     [SerializeField] private float jumpHight;
  9.  
  10.     [SerializeField] private int jumpcount;
  11.  
  12.     [SerializeField] private GameObject runparticelsPrefab;
  13.  
  14.    
  15.  
  16.     [SerializeField]
  17.     LayerMask platformslayermask;//only hit on platforms
  18.     SpriteRenderer spriteRenderer;
  19.     Animator anim;
  20.     Rigidbody2D rigidBody2D;
  21.     BoxCollider2D boxcollider2D;
  22.     PlayerAnimationEvents playerAnimEvents;
  23.  
  24.  
  25.  
  26.  
  27.     // Start is called before the first frame update
  28.     void Start()
  29.     {
  30.         jumpcount = 0;
  31.         CentralController.cencon.playerobj = this.gameObject;
  32.  
  33.         anim = this.gameObject.GetComponent<Animator>();
  34.         spriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
  35.         rigidBody2D = this.gameObject.transform.GetComponent<Rigidbody2D>();
  36.         boxcollider2D = this.gameObject.transform.GetComponent<BoxCollider2D>();
  37.         playerAnimEvents = GameObject.Find("MainPlayer").GetComponent<PlayerAnimationEvents>();
  38.     }
  39.  
  40.         //Update is called once per frame
  41.     void Update()
  42.     {
  43.         if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0)
  44.         {
  45.             PlayerInput(new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0));
  46.         }
  47.         else
  48.         {
  49.             anim.SetBool("IsRunning", false);
  50.         }
  51.  
  52.         if (Input.GetButtonDown("Jump"))
  53.         {
  54.             if (jumpcount < 2)
  55.             {
  56.                 PlayerJump();
  57.                 playerAnimEvents.JumpDust();
  58.             }
  59.         }
  60.         else if (CheckIfGrounded() && jumpcount > 0)
  61.         {
  62.             anim.SetBool("IsJumpActive", false);
  63.             jumpcount = 0;
  64.         }
  65.         //PlayerInput(new Vector3(0,0,0));
  66.     }
  67.  
  68.     private void PlayerJump()
  69.     {
  70.         Vector2 jump = Vector2.up * jumpHight;
  71.         rigidBody2D.velocity = jump;
  72.         anim.SetBool("IsJumpActive", true);
  73.  
  74.         jumpcount++;
  75.     }
  76.  
  77.     public void PlayerInput(Vector3 movementInput)
  78.     {
  79.         if (movementInput.x > 0)
  80.         {
  81.             spriteRenderer.flipX = false;
  82.             runparticelsPrefab.GetComponent<SpriteRenderer>().flipX = false;  
  83.         }
  84.         else
  85.         {
  86.             spriteRenderer.flipX = true;
  87.             runparticelsPrefab.GetComponent<SpriteRenderer>().flipX = true;          
  88.         }
  89.         this.gameObject.transform.position += movementInput * walkingspeed * Time.deltaTime;
  90.         anim.SetBool("IsRunning", true);
  91.  
  92.  
  93.         //if (Input.GetAxisRaw("Horizontal") > 0)
  94.         //{
  95.         //    anim.SetBool("IsRunning", true);
  96.         //    spriteRenderer.flipX = false;
  97.         //    this.gameObject.transform.position += Vector3.right * walkingspeed * Time.deltaTime;
  98.         //}
  99.         //else if (Input.GetAxisRaw("Horizontal") < 0)
  100.         //{
  101.         //    anim.SetBool("IsRunning", true);
  102.         //    spriteRenderer.flipX = true;
  103.         //    this.gameObject.transform.position += Vector3.left * walkingspeed * Time.deltaTime;
  104.         //}
  105.         //else
  106.         //{
  107.         //    anim.SetBool("IsRunning", false);
  108.         //}
  109.  
  110.         //this.gameObject.transform.position += newpos.normalized * walkingspeed * Time.deltaTime;
  111.  
  112.         //if (CheckIfGrounded() && Input.GetButtonDown("Jump"))
  113.         //{
  114.         //    Vector2 jump = Vector2.up * jumpHight;
  115.         //    rigidBody2D.velocity = jump;
  116.         //    anim.SetBool("IsJumpActive", true);
  117.         //}
  118.         //else if (rigidBody2D.velocity.y == 0)
  119.         //{
  120.         //    anim.SetBool("IsJumpActive", false);
  121.         //}
  122.     }
  123.  
  124.     private bool CheckIfGrounded()
  125.     {
  126.         RaycastHit2D raycastHit2D = Physics2D.BoxCast(boxcollider2D.bounds.center, boxcollider2D.bounds.size, 0f, Vector2.down, .1f, platformslayermask);
  127.         return raycastHit2D.collider != null;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement