'Bullet script problem in unity: transform is not responding

I'm trying to use a script to make bullets spawn and move but they just spawn and drop to the ground due to the gravity on the rigidbody, but no gravity seems to be applied. I'm suspecting I need to declare something I didn't declare, but I'm pretty new to coding so I can't seem to solve it. Here's the script I've got so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoot : MonoBehaviour
{
    public GameObject projectile;
    public float speed = 1000;
    
   
    void Start()
    {
       
    }
 
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Mouse0))
        {
            

            Instantiate(projectile, transform.position, projectile.transform.rotation);

            projectile.transform.Translate(Vector3.back * Time.deltaTime * speed);

            Debug.Log("Shoot!");
        }
    }
}

And here's a screen recording of the problem:

https://drive.google.com/file/d/14KTSx7fJMhs-se40sZFDFmTJGv0Kqylk/view?usp=sharing

projectile refers to the bullet prefab I've created and attached to the spawnPoint gameObject, which as you can see it's on the tip of the rifle

Any help would be highly appreciated!



Solution 1:[1]

There are multiple issues here

  • You spawn a new copy of (I assume) the prefab projectile but then instead of moving this new spawned object you try to move the original prefab projectile.

    ? You rather want to move the new spawned object instead!

  • you move only for the one single frame where GetKeyDown is true.

    ? For a continuous movement you would need to keep calling the movement continously once every frame!

    So what you would (see bottom point) be doing is rather have a dedicated component on the projectile prefab

    public class Bullet : MonoBehaviour
    {
        public float speed = 1000;
    
        private void Update ()
        {
            // might have to tweak the direction e.g. Vector3.forward according to your needs
            transform.Translate(Vector3.back * Time.deltaTime * speed);
        }
    }
    

    And then simply do

    public class Shoot : MonoBehaviour
    {
        public Bullet projectile;
    
        void Update()
        {
            if(Input.GetKeyDown(KeyCode.Mouse0))
            {      
                Instantiate(projectile, transform.position, transform.rotation);
    
                Debug.Log("Shoot!");
            }
        }
    }
    
  • However, since you are using a Rigidbody and want to move your bullet using Physics and get a working Collision Detection you shouldn't use Transform at all.

    Rather disable the gravity on the Rigidbody and then do

    public class Shoot : MonoBehaviour
    {
        public Rigidbody projectile;
        public float speed = 1000;
    
        void Update()
        {
            if(Input.GetKeyDown(KeyCode.Mouse0))
            {      
                var newBullet = Instantiate(projectile, transform.position, transform.rotation);
                // Might have to tweak the direction using -transform.forward according to your needs
                newBullet.velocity = transform.forward * speed;
    
                Debug.Log("Shoot!");
            }
       }
    }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1