How to make a projectile use world space?

Hi, i’m trying to make a projectile script, but i have a problem. When a instantiate the projectile, he uses Player’s transform as reference, so if the Player rotate or walk after shooting, the projectile will rotate and/or walk with him. I tried use “transform.parent = null” but my projectile now doesn’t hit anything.

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

public class PlayerShooting : MonoBehaviour {

    public GameObject projectile;
    public AudioSource gunFireAudio;
    public ParticleSystem gunFireParticle;

    SuperBoyMovement movement;
    Animator anim;

    // Use this for initialization
    void Start () {
        movement = GetComponent<SuperBoyMovement>();
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Fire1"))
        {
            gunFireAudio.Play();
            gunFireParticle.Play();
            anim.SetBool("isShooting", true);
            Instantiate(projectile, transform);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour
{

    public float projectileSpeed;
    public float rangeTime;
    public int damage;

    float timer;
    EnemyHealth enemy;
    SuperBoyHealth player;

    void Start()
    {
        //transform.parent = null;
    }

    void Update()
    {
        timer += Time.deltaTime;
        transform.Translate(0, 0, projectileSpeed * Time.deltaTime);
        if (timer >= rangeTime)
        {
            Destroy(gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Wall")
        {
            Destroy(gameObject);
        }
        else if (other.tag == "Enemy")
        {
            enemy = other.GetComponent<EnemyHealth>();
            if(enemy != null)
            {
                enemy.TakeDamage(damage);
                Destroy(gameObject);
            }
        }
    }
}

I haven’t looked at your code, but your bullet definitely should NOT be a child of your player, so definitely don’t do that (slow your bullet speed way down and then look in the hierarchy after you shoot to see if it’s a child of the player or anything, to be sure).

If it’s not hitting anything, it sounds like you either don’t have a collider/trigger on it, or you have a collider/trigger on it but haven’t scripted it and with the script on the bullet. Make sure your projectile has the script checking for the collision. You could add Debug.Log outputs to your on trigger check too to see if it actually triggered.

Hello, first of all thanks for your answer!
I understand that the bullet should not be a child of player, but the only way i succeded in removing it from his parent it’s using " transform.parent = null; " , but like i said, when i do that, the bullet ignores the void OnTriggerEnter, i have already checked and the bullet have a Collider with “Is Trigger” checked. I must be missing something, but i have no idea of what.

Nevermind, it worked using transform.parent = null and putting an RigidBody at the bullet.

That’s good it works, although I will say you actually shouldn’t even need to set transform.parent = null. The thing is you’re instantiating your bullet as a child of the player the way you’re doing it, when really you should just instantiate the bullet at the player’s location (or whatever location you shoot from). Rather than doing:

Instantiate(projectile, transform); // instantiate bullet, and assign it a parent

You could do:

Instantiate(projectile, transform.position, transform.rotation); //instantiate a bullet, and place it at the transform's location, with same rotation

They look similar, but the first way of doing it is actually assigning it a parent, whereas the second is just putting it at that transform’s location, with same rotation.

Anyways, your way works too, but just showing you the way that would be a little better to do. A little more clear as to what’s happening, if someone else looks at your code, for example.

More info here: Unity - Scripting API: Object.Instantiate

Thank you very much for taking the time to explain me this! Makes it very clear to me what was the problem. My “solution” was more luck than realy understanding why it isn’t working at the first place.

1 Like