How do I change velocity of a cloned prefab outside of a function?

Hello, I am new to unity and I’m trying to make a 2D shooter game. I have made a function that instantiates a bullet when space is pressed, but I want to make the bullets decelerate after being shot. I’m not sure how to do this since I’m not sure if you’re able to modify the bulletClone.velocity since it’s a local variable

Here’s my code, any help would be very much appreciated!

using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.UIElements;
using static UnityEditor.PlayerSettings;

public class PlayerMovement : MonoBehaviour
{
    // game objects / components
    public GameObject Player;
    public Rigidbody2D playerRB;
    public Rigidbody2D bulletRB;
    public GameObject bullet;

    // variables
    public float movespeed = 1.5f;
    public float firespeed = 5f;
    public float inputX = 0f;
    public float inputY = 0f;
    public float bulletDecelerate = 1f;
    public bool bulletIsAlive = false;
    public float reloadSpeed = 1;
    public float shotCooldown = 0;



    public Rigidbody2D shoot()
    {

        Debug.Log("shot");

        // instantiate the bullet at the player's position

        
        Rigidbody2D bulletClone;
        bulletClone = Instantiate(bulletRB, playerRB.position, Quaternion.identity);
        
        // shoot bullet in same direction as player

        Vector2 bulletCloneVelocity = new Vector2(inputX, inputY);  
        bulletClone.velocity = bulletCloneVelocity;

        bulletIsAlive = true;

        //return bulletClone;

        return bulletClone;



    }
        
    public void dash()
    {
        // add a dash function. at the start of the game, the player has no gun and must dash into enemies to kill them. dash cooldown. if not dashing you will take damage.
    }


    void Start()
    {
        playerRB = Player.GetComponent<Rigidbody2D>();
        bulletRB = bullet.GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // player movement
        inputX = Input.GetAxis("Horizontal");
        inputY = Input.GetAxis("Vertical");
        Vector2 moveVector = new Vector2(inputX, inputY);
        playerRB.position += moveVector * (movespeed * Time.deltaTime);

        // if button is pressed, shoot
        if (Input.GetKey(KeyCode.Space) && (shotCooldown >= reloadSpeed))
        {
            shoot();
            shotCooldown = 0;
        }

        // if any bullets have been shot, reduce their speed and destroy if speed = 0
        if (bulletIsAlive) 
        {
            // reduce speed 
            bulletClone.velocity -= bulletDecelerate;
        }

        shotCooldown += 1 * Time.deltaTime;
    }
}

You should write a component that you put on the bullet itself that slows it down.

Also you should remove the using static UnityEditor.PlayerSettings; from your script as it will prevent you from building your game.

Thanks for the reply,

So do you mean I should just add a line that reduces bulletRB.velocity, and since the instantiated versions are clones of this will their velocity also be reduced?

No I mean write a new monobehaviour script that does this specific behaviour, and put that new component on your bullet prefab game object.

Ah okay that makes sense, thanks for your help :slight_smile:

1 Like

As mentioned, it’s good practice to have behaviors on the spawned objects themselves, like decelerate, checking when they should be destroyed, etc. That said, there are cases where the instantiating script needs access to those spawned objects later on (some action happens that’s not known by the spawned object and you need to modify one or more of them). In that case you’d create a GameObject list (global to the instantiating script) and as you spawn objects, put each into the list. Then you can modify public properties or run public functions in those spawned objects from the main script anytime you need. But anything that’s “set it and forget it” or “start it and it’ll do its thing” should be handled by the object itself.

1 Like