Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x3
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- Animator animator;
- Rigidbody2D rb2d;
- SpriteRenderer spriteRenderer;
- bool isGrounded;
- [SerializeField]
- Transform groundCheck;
- [SerializeField]
- private float runspeed = 1.5f;
- [SerializeField]
- private float jumpspeed = 4f;
- // Start is called before the first frame update
- void Start()
- {
- animator = GetComponent<Animator>();
- rb2d = GetComponent<Rigidbody2D>();
- spriteRenderer = GetComponent<SpriteRenderer>();
- }
- // Update is called once per frame
- void Update()
- {
- }
- private void FixedUpdate()
- {
- if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
- {
- isGrounded = true;
- }
- else
- {
- isGrounded = false;
- }
- print("isGrounded:" + isGrounded);
- if (Input.GetKey("right"))
- {
- rb2d.velocity = new Vector2(runspeed, rb2d.velocity.y);
- if (isGrounded)
- animator.Play("Player_walk");
- spriteRenderer.flipX = false;
- }
- else if (Input.GetKey("left"))
- {
- rb2d.velocity = new Vector2(-runspeed, rb2d.velocity.y);
- if (isGrounded)
- animator.Play("Player_walk");
- spriteRenderer.flipX = true;
- }
- else
- {
- rb2d.velocity = new Vector2(0, rb2d.velocity.y);
- if (isGrounded)
- {
- animator.Play("Player_idle");
- }
- }
- if (Input.GetKeyDown("down") && isGrounded)
- {
- animator.Play("Player_duck");
- }
- else if (Input.GetKeyUp("down"))
- {
- animator.Play("Player_idle");
- }
- if (Input.GetKey("space") && isGrounded)
- {
- rb2d.velocity = new Vector2(rb2d.velocity.x, jumpspeed);
- animator.Play("Player_jump");
- }
- }
- }
- x3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement