WolfGamesProgrammer

Untitled

Jan 28th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     [SerializeField] float speed = 2;
  8.     [SerializeField] float jumpPower = 5;
  9.     Rigidbody2D rigidbody2D;
  10.     float moving;
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.         rigidbody2D = GetComponent<Rigidbody2D>();
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.         moving = Input.GetAxis("Horizontal");
  21.         transform.position += new Vector3(speed * Time.deltaTime * moving, 0, 0);
  22.         if (Input.GetKeyDown(KeyCode.Space))
  23.         {
  24.             Jump();
  25.         }
  26.     }
  27.     void Jump()
  28.     {
  29.         rigidbody2D.AddForce(transform.up * jumpPower, ForceMode2D.Impulse);
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment