MrsMcLead

PlayerController2D

Apr 11th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //using UnityEngine.SceneManagement;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.  
  9. private Rigidbody2D rb;
  10. public float speed;
  11. public Vector2 jumpHeight;
  12. public bool isGrounded;
  13.  
  14. // Use this for initialization
  15. void Start()
  16. {
  17. rb = GetComponent<Rigidbody2D>();
  18. isGrounded = false;
  19. }
  20.  
  21. // Update is called once per frame
  22. void FixedUpdate()
  23. {
  24. float moveHorizontal = Input.GetAxis("Horizontal");
  25. Vector2 movement = new Vector2(moveHorizontal, 0);
  26. rb.AddForce(movement * speed);
  27. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  28. {
  29. rb.AddForce(jumpHeight, ForceMode2D.Impulse);
  30. isGrounded = false;
  31. // SceneManager.LoadScene("win");
  32. }
  33.  
  34.  
  35. }
  36.  
  37. private void OnCollisionEnter2D(Collision2D collision)
  38. {
  39. if(collision.gameObject.tag == "Ground")
  40. {
  41. isGrounded = true;
  42. }
  43. }
  44. }
Add Comment
Please, Sign In to add comment