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
- {
- private Rigidbody2D playerBody;
- public float speed = 5f;
- private Animator animationController;
- public bool isFacingRight = true;
- public bool isGrounded = true;
- public float jumpForce = 10f;
- private Vector2 moveAmount;
- void Start()
- {
- playerBody = GetComponent<Rigidbody2D>();
- animationController = GetComponent<Animator>();
- }
- void Update()
- {
- float movementAxis = Input.GetAxisRaw("Horizontal");
- moveAmount = new Vector2(movementAxis, playerBody.velocity.y).normalized;
- if (movementAxis > 0 || movementAxis < 0)
- {
- animationController.SetBool("isRunning", true);
- Flip(movementAxis);
- }
- else
- animationController.SetBool("isRunning", false);
- // if (Input.GetButton("Jump")&& isGrounded)
- // {
- // playerBody.AddForce(Vector2.up * jumpForce*Time.deltaTime);
- // isGrounded = false;
- // }
- }
- private void Flip(float movementAxis)
- {
- if (movementAxis < 0)
- {
- transform.localRotation = Quaternion.Euler(0, 180, 0);
- }
- else if(movementAxis>0)
- {
- transform.localRotation = Quaternion.Euler(0, 0, 0);
- }
- }
- public void IsGrounded(bool grounded)
- {
- isGrounded = grounded;
- }
- private void FixedUpdate()
- {
- playerBody.MovePosition(playerBody.position + moveAmount * speed * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement