Advertisement
leomovskii

Movement.cs

Aug 27th, 2022
992
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 1 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Movement : MonoBehaviour {
  6.  
  7.     public Rigidbody2D rb;
  8.  
  9.     public float speed = 7f;
  10.     public float jumpPower = 300f;
  11.  
  12.     bool onGround;
  13.  
  14.     void Start() {
  15.        
  16.     }
  17.  
  18.     void Update() {
  19.         float x = Input.GetAxis("Horizontal");
  20.         rb.velocity = new Vector2(speed * x, rb.velocity.y);
  21.  
  22.         if (x > 0) {
  23.             transform.eulerAngles = new Vector3(0,0,0);
  24.         } else if (x < 0) {
  25.             transform.eulerAngles = new Vector3(0,180,0);
  26.         }
  27.  
  28.         if (Input.GetButtonDown("Jump") && onGround) {
  29.             rb.AddForce(Vector2.up * jumpPower);
  30.             onGround = false;
  31.         }
  32.     }
  33.  
  34.     void OnCollisionEnter2D(Collision2D c) {
  35.         onGround = true;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement