Advertisement
MrsMcLead

PlayerController

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