Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- //using UnityEngine.SceneManagement;
- public class PlayerController : MonoBehaviour
- {
- private Rigidbody2D rb;
- public float speed;
- public Vector2 jumpHeight;
- public bool isGrounded;
- // Use this for initialization
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- isGrounded = false;
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- float moveHorizontal = Input.GetAxis("Horizontal");
- Vector2 movement = new Vector2(moveHorizontal, 0);
- rb.AddForce(movement * speed);
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- {
- rb.AddForce(jumpHeight, ForceMode2D.Impulse);
- isGrounded = false;
- // SceneManager.LoadScene("win");
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if(collision.gameObject.tag == "Ground")
- {
- isGrounded = true;
- }
- }
- }
Add Comment
Please, Sign In to add comment