Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PaddleMove : MonoBehaviour
- {
- public float speed = 5f;
- public GameObject bullet;
- public int fireRate;
- float timeFromLastShoot = 0;
- void Update()
- {
- Move();
- Shoot();
- }
- //Movement function
- void Move()
- {
- //Get which way we want to move from GetAxisRaw
- float x = Input.GetAxisRaw("Horizontal");
- //calculate the speed in the set direction
- float speedDir = x * speed * Time.deltaTime;
- transform.position += new Vector3(speedDir, 0, 0);
- }
- void Shoot()
- {
- //calculating time from last shot
- timeFromLastShoot += Time.deltaTime;
- //calculating where should the bullet appear
- Vector3 pos = transform.position + new Vector3(0, 1, 0);
- //if the time from the last shot is long enough
- if (timeFromLastShoot >= (1 / fireRate))
- {
- //After pressing the fire button
- if (Input.GetButtonDown("Fire1"))
- {
- //create a bullet
- Instantiate(bullet, pos, Quaternion.identity);
- //reset time from last shot
- timeFromLastShoot = 0;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment