Advertisement
janevim

Player

May 2nd, 2023 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     //player must have a rigidbody2D and a box colider
  8.     public float moveSpeed = 5f;
  9.  
  10.     // Start is called before the first frame update
  11.     void Start()
  12.     {
  13.     }
  14.  
  15.     // Update is called once per frame
  16.     void Update()
  17.     {
  18.         Jump();
  19.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
  20.         transform.position += movement * Time.deltaTime * moveSpeed;
  21.     }
  22.  
  23.     void Jump()
  24.     {
  25.         if (Input.GetButtonDown("Jump"))
  26.         {
  27.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
  28.         }
  29.     }
  30.     void Shoot()
  31.     {
  32.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
  33.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
  34.         rb.AddForce(firePoint.right * bulletSpeed, ForceMode2D.Impulse);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement