How to fire projectile in direction character is facing?

Hi all,

I have this script that successfully shoots projectiles by instantiating it and deleting it 3 seconds later to keep the game running smoothly.

However, the projectiles will only shoot in one direction (Z axis). If I turn left, the projectile will still be fired in the Z axis.

Script below:

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

public class FireExtingControls : MonoBehaviour
{
    // Script should be attached to spawn point of foam rather than foam itself!!
    public GameObject foam;
    public float speed = 10f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            GameObject instFoam = Instantiate(foam, transform.position, Quaternion.identity);
            Rigidbody instFoamRB = instFoam.GetComponent<Rigidbody>();

            instFoamRB.AddForce(Vector3.forward * speed);
            Destroy(instFoam, 3f);
        }


            

    }
}

What I want is that the projectile will shoot in the direction the player is looking, be it left, right, up or down.

Thanks in advance :slight_smile:

On line 25 instead of
instFoamRB.AddForce(Vector3.forward * speed);
you should write instFoamRB.AddForce(gameObject.transform.forward * speed);

Vector3.forward is simply (0,0,1) whereas gameObject.transform.forward is the vector that you want.

Update:

attach this script to any game object that is capable of triggering projectiles:

using UnityEngine;

/// <summary>
/// The class definition for a projectile's trigger
/// </summary>
/// <remarks>
/// Attach this script as a component to any object capable of triggering projectiles
/// The spawn transform should represent the position where the projectile is to appear, i.e. gun barrel end
/// </remarks>
public class ProjectileTrigger : MonoBehaviour
{
    /// <summary>
    /// Public fields
    /// </summary>
    public GameObject m_Projectile;    // this is a reference to your projectile prefab
    public Transform m_SpawnTransform; // this is a reference to the transform where the prefab will spawn

    /// <summary>
    /// Message that is called once per frame
    /// </summary>
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(m_Projectile, m_SpawnTransform.position, m_SpawnTransform.rotation);
        }
    }
}

attach this script to any prefab that represents a projectile:

using UnityEngine;

/// <summary>
/// The class definition for a projectile
/// </summary>
[RequireComponent(typeof(Rigidbody))]
public class Projectile : MonoBehaviour
{
    /// <summary>
    /// Public fields
    /// </summary>
    public float m_Speed = 10f;   // this is the projectile's speed
    public float m_Lifespan = 3f; // this is the projectile's lifespan (in seconds)

    /// <summary>
    /// Private fields
    /// </summary>
    private Rigidbody m_Rigidbody;

    /// <summary>
    /// Message that is called when the script instance is being loaded
    /// </summary>
    void Awake()
    {
        m_Rigidbody = GetComponent<Rigidbody>();
    }

    /// <summary>
    /// Message that is called before the first frame update
    /// </summary>
    void Start()
    {
        m_Rigidbody.AddForce(m_Rigidbody.transform.forward * m_Speed);
        Destroy(gameObject, m_Lifespan);
    }
}

m_Rigidbody.AddForce(transform.forward * m_Speed);

Working by only change this line.

Thank you