coding_giants

PaddleMove

Mar 26th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PaddleMove : MonoBehaviour
  6. {
  7.     public float speed = 5f;
  8.     public GameObject bullet;
  9.     public int fireRate;
  10.     float timeFromLastShoot = 0;
  11.  
  12.     void Update()
  13.     {
  14.         Move();
  15.         Shoot();
  16.     }
  17.  
  18.     //Movement function
  19.     void Move()
  20.     {
  21.         //Get which way we want to move from GetAxisRaw
  22.         float x = Input.GetAxisRaw("Horizontal");
  23.         //calculate the speed in the set direction
  24.         float speedDir = x * speed * Time.deltaTime;
  25.         transform.position += new Vector3(speedDir, 0, 0);
  26.     }
  27.  
  28.     void Shoot()
  29.     {
  30.         //calculating time from last shot
  31.         timeFromLastShoot += Time.deltaTime;
  32.         //calculating where should the bullet appear
  33.         Vector3 pos = transform.position + new Vector3(0, 1, 0);
  34.         //if the time from the last shot is long enough
  35.         if (timeFromLastShoot >= (1 / fireRate))
  36.         {
  37.             //After pressing the fire button
  38.             if (Input.GetButtonDown("Fire1"))
  39.             {
  40.                 //create a bullet
  41.                 Instantiate(bullet, pos, Quaternion.identity);
  42.                 //reset time from last shot
  43.                 timeFromLastShoot = 0;
  44.             }
  45.         }
  46.     }
  47. }
Add Comment
Please, Sign In to add comment