How can I make an ogameobject to move smooth forward nonsotp after mouse button click ?

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class ShootBullets : MonoBehaviour
{
    public GameObject bulletPrefab;
    public float bulletSpeed;
    public float bulletVelocity;
    public bool automaticFire = false;
    public float fireRate;

    private float gunheat;
    private List<GameObject> startPositions;
    private bool shoot = false;
    private GameObject bulletsParent;
    private GameObject mybullet;

    // Start is called before the first frame update
    void Start()
    {
        startPositions = GameObject.FindGameObjectsWithTag("Bullet").ToList();
        bulletsParent = GameObject.Find("Bullets");
    }

    // Update is called once per frame
    void Update()
    {
        if (shoot == true)
        {
            mybullet.transform.position += mybullet.transform.forward * 20 * Time.deltaTime;

        }

        if (automaticFire == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                for (int i = 0; i < startPositions.Count; i++)
                {
                    GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity, bulletsParent.transform) as GameObject;
                    mybullet = bullet;
                    shoot = true;

                    Destroy(bullet, 10f);
                }
            }
        }
        else
        {
            if (shoot == true)
            {
                for (int i = 0; i < startPositions.Count; i++)
                {
                    GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity) as GameObject;
                    Destroy(bullet, 0.5f);

                    shoot = false;
                }
            }
        }

        if (gunheat > 0) gunheat -= Time.deltaTime;

        if (gunheat <= 0)
        {
            shoot = true;
            gunheat = fireRate;
        }
    }
}

The problem is that the GameObject bullet is created when the mouse left button clicked so I can’t use the bullet inside the Update. I tried to use a helper GameObject name mybullet and get reference of bullet in the mouse button left click but mybullet is null when shoot is true.

I want that when clicking the mouse left button it will shoot a bullet that will move forward nonstop smooth.

Well, the easiest way would be to just give the bullet a direction when you create it and then let the bullet to all the work. To do so, you can create a bullet prefab that already has a move script attached to it and you just move it by X per frame. Though Vector3.MoveTowards probably works better than flat out changing the position.

1 Like

I created a script for the bullet to move it using MoveTowards but it’s moving the bullet to the right and not forward.

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

public class MoveBullet : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, transform.forward, 10 * Time.deltaTime);
    }
}

The blue axis direction is to the left the red direction is to the back and the green is up.
but the bullet is moving to the right the other direction of the blue.

and I want the bullet to move forward the other way of the red.

Well, you make it inherit the direction of the object that creates it. You can turn it around before shooting it into any direction, either when you instantiate() it, or in the Start() method. Personally, I would use the instantiate(), as that makes it easier to adapt. You will need to replace the Quaternion.identity with the direction you need it to go into. You might need to experiment with that a bit.

1 Like

What should I use ? I tried :

Quaternion.LookRotation

Tried Vector3 forward and Vector3 up but it didn’t change anything.

You know where the bullet goes when you use Quaternion.identity. You know where you want to go. Now you need to figure out how to turn it into that direction. I think that should be 90 degrees around the y axis, but as I said, you would need to figure that one out by trying. Unfortunately, I’m not that well versed with Quaternions myself, to tell you how to do that exactly, but it’s where you need to start looking.

Basically, take the identity direction, because you know where that is, then add the rotation you need to get the right direction. Look up how Quaternions work.

Something like this should do the trick.

Blue is forward, red is right and green is up. This is engine specific, it is a left handed coordinate system.

transform.position = Vector3.MoveTowards(transform.position, transform.forward, 10 * Time.deltaTime);

This will move the bullet to near world origin. Forward will be a unit vector meaning it cannot have a magnitude greater than one. This value is (0, 0, 1) by default so it will going 1 unit away from world origin. Move it the same way you did in the spawning script.

You will need to change this:

GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity, bulletsParent.transform) as GameObject;

To this.

GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, startPositions[i].transform.rotation, null) as GameObject;

You can leave the parent null, its not needed.