Search Unity

Question My bullets won't fly further than 1-2m.[ANSWERED]

Discussion in 'Getting Started' started by w1857926, Feb 18, 2024.

  1. w1857926

    w1857926

    Joined:
    Nov 2, 2023
    Posts:
    2
    Hi, I am doing a coursework and I follow the tutorials from my lecturers, but unfortunately I cannot change the fire rate of the bullets. Here is my code:


    Code (CSharp):
    1. Player.cs
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     public float movementSpeed = 10f;
    10.     Rigidbody rigidBody;
    11.  
    12.     public float fireRate = 0.75f;
    13.     public GameObject bulletPrefab;
    14.     public Transform bulletPosition;
    15.     float nextFire;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         rigidBody = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void FixedUpdate()
    24.     {
    25.         Move();
    26.  
    27.         if (Input.GetKey(KeyCode.Space))
    28.         {
    29.             Fire();
    30.         }
    31.     }
    32.  
    33.  
    34.     void Move()
    35.     {
    36.         if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
    37.             return;
    38.  
    39.         var horizontalInput = Input.GetAxis("Horizontal");
    40.         var verticalInput = Input.GetAxis("Vertical");
    41.  
    42.         var rotation = Quaternion.LookRotation(new Vector3(horizontalInput, 0 , verticalInput));
    43.         transform.rotation = rotation;
    44.  
    45.         Vector3 movementDir = transform.forward * movementSpeed * Time.deltaTime;
    46.         rigidBody.MovePosition(rigidBody.position + movementDir);
    47.     }
    48.  
    49.     void Fire()
    50.     {
    51.         if (Time.time > nextFire)
    52.         {
    53.             nextFire = Time.time + fireRate;
    54.  
    55.             GameObject bullet = Instantiate(bulletPrefab, bulletPosition.position, Quaternion.identity);
    56.  
    57.             bullet.GetComponent<BulletController>()?.InitializeBullet(transform.rotation * Vector3.forward);
    58.         }
    59.     }
    60.  
    61.  
    62. }
    63.  
    Code (CSharp):
    1. BulletController
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class BulletController : MonoBehaviour
    8. {
    9.     Rigidbody rigidBody;
    10.  
    11.     public float bulletSpeed = 15f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Awake()
    15.     {
    16.         rigidBody = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.    
    23.     }
    24.  
    25.     public void InitializeBullet(Vector3 originalDirection)
    26.     {
    27.         transform.forward = originalDirection;
    28.         rigidBody.velocity = transform.forward * bulletSpeed;
    29.     }
    30.  
    31.     private void OnCollisionEnter(Collision collision)
    32.     {
    33.         Destroy(gameObject);
    34.     }
    35. }

    Also, when I shoot the first bullet and it take some seconds until I can shoot again, like 3-5 sec.

    Thank you in advance.
     
  2. w1857926

    w1857926

    Joined:
    Nov 2, 2023
    Posts:
    2
    I found the problem. I forgot to uncheck Use Gravity on Rigid Body of the bullet.