Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7. private Rigidbody2D playerBody;
  8. public float speed = 5f;
  9. private Animator animationController;
  10. public bool isFacingRight = true;
  11. public bool isGrounded = true;
  12. public float jumpForce = 10f;
  13. private Vector2 moveAmount;
  14. void Start()
  15. {
  16. playerBody = GetComponent<Rigidbody2D>();
  17. animationController = GetComponent<Animator>();
  18. }
  19.  
  20. void Update()
  21. {
  22. float movementAxis = Input.GetAxisRaw("Horizontal");
  23. moveAmount = new Vector2(movementAxis, playerBody.velocity.y).normalized;
  24. if (movementAxis > 0 || movementAxis < 0)
  25. {
  26. animationController.SetBool("isRunning", true);
  27. Flip(movementAxis);
  28. }
  29. else
  30. animationController.SetBool("isRunning", false);
  31.  
  32. // if (Input.GetButton("Jump")&& isGrounded)
  33. // {
  34. // playerBody.AddForce(Vector2.up * jumpForce*Time.deltaTime);
  35. // isGrounded = false;
  36. // }
  37. }
  38.  
  39. private void Flip(float movementAxis)
  40. {
  41. if (movementAxis < 0)
  42. {
  43. transform.localRotation = Quaternion.Euler(0, 180, 0);
  44. }
  45. else if(movementAxis>0)
  46. {
  47. transform.localRotation = Quaternion.Euler(0, 0, 0);
  48. }
  49.  
  50. }
  51.  
  52. public void IsGrounded(bool grounded)
  53. {
  54. isGrounded = grounded;
  55. }
  56.  
  57. private void FixedUpdate()
  58. {
  59. playerBody.MovePosition(playerBody.position + moveAmount * speed * Time.deltaTime);
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement