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 PlayerMovement : MonoBehaviour
- {
- [SerializeField] private float walkingspeed;
- [SerializeField] private float jumpHight;
- [SerializeField] private int jumpcount;
- [SerializeField] private GameObject runparticelsPrefab;
- [SerializeField]
- LayerMask platformslayermask;//only hit on platforms
- SpriteRenderer spriteRenderer;
- Animator anim;
- Rigidbody2D rigidBody2D;
- BoxCollider2D boxcollider2D;
- PlayerAnimationEvents playerAnimEvents;
- // Start is called before the first frame update
- void Start()
- {
- jumpcount = 0;
- CentralController.cencon.playerobj = this.gameObject;
- anim = this.gameObject.GetComponent<Animator>();
- spriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
- rigidBody2D = this.gameObject.transform.GetComponent<Rigidbody2D>();
- boxcollider2D = this.gameObject.transform.GetComponent<BoxCollider2D>();
- playerAnimEvents = GameObject.Find("MainPlayer").GetComponent<PlayerAnimationEvents>();
- }
- //Update is called once per frame
- void Update()
- {
- if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0)
- {
- PlayerInput(new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0));
- }
- else
- {
- anim.SetBool("IsRunning", false);
- }
- if (Input.GetButtonDown("Jump"))
- {
- if (jumpcount < 2)
- {
- PlayerJump();
- playerAnimEvents.JumpDust();
- }
- }
- else if (CheckIfGrounded() && jumpcount > 0)
- {
- anim.SetBool("IsJumpActive", false);
- jumpcount = 0;
- }
- //PlayerInput(new Vector3(0,0,0));
- }
- private void PlayerJump()
- {
- Vector2 jump = Vector2.up * jumpHight;
- rigidBody2D.velocity = jump;
- anim.SetBool("IsJumpActive", true);
- jumpcount++;
- }
- public void PlayerInput(Vector3 movementInput)
- {
- if (movementInput.x > 0)
- {
- spriteRenderer.flipX = false;
- runparticelsPrefab.GetComponent<SpriteRenderer>().flipX = false;
- }
- else
- {
- spriteRenderer.flipX = true;
- runparticelsPrefab.GetComponent<SpriteRenderer>().flipX = true;
- }
- this.gameObject.transform.position += movementInput * walkingspeed * Time.deltaTime;
- anim.SetBool("IsRunning", true);
- //if (Input.GetAxisRaw("Horizontal") > 0)
- //{
- // anim.SetBool("IsRunning", true);
- // spriteRenderer.flipX = false;
- // this.gameObject.transform.position += Vector3.right * walkingspeed * Time.deltaTime;
- //}
- //else if (Input.GetAxisRaw("Horizontal") < 0)
- //{
- // anim.SetBool("IsRunning", true);
- // spriteRenderer.flipX = true;
- // this.gameObject.transform.position += Vector3.left * walkingspeed * Time.deltaTime;
- //}
- //else
- //{
- // anim.SetBool("IsRunning", false);
- //}
- //this.gameObject.transform.position += newpos.normalized * walkingspeed * Time.deltaTime;
- //if (CheckIfGrounded() && Input.GetButtonDown("Jump"))
- //{
- // Vector2 jump = Vector2.up * jumpHight;
- // rigidBody2D.velocity = jump;
- // anim.SetBool("IsJumpActive", true);
- //}
- //else if (rigidBody2D.velocity.y == 0)
- //{
- // anim.SetBool("IsJumpActive", false);
- //}
- }
- private bool CheckIfGrounded()
- {
- RaycastHit2D raycastHit2D = Physics2D.BoxCast(boxcollider2D.bounds.center, boxcollider2D.bounds.size, 0f, Vector2.down, .1f, platformslayermask);
- return raycastHit2D.collider != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement