3D Top Down shoot to mousePosition - projectile going through the floor

Hey guys,

So this is a 3D top-down game. I’m instantiating a projectile and want it to travel towards my mouse position when I click. This is working fine, but what’s driving me crazy is that the projectile is also going through the floor. As soon as i mess with the Y axis in my direction Vector, then the projectile direction angles get more messed up and don’t actually go perfectly straight towards my mouse click. My end goal is to have the projectile perfectly align with the mouse pointer, but have the projectile run parallel to the ground.

Any suggestions?

Code is below:

TestSpell Class is attached to character.
ShootProjectile Class is attached to the projectile prefab

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

public class TestSpell : MonoBehaviour {

    public float impacyDamage;
    public float manaCost;
    public bool pierceEnemy;
    public float projectileSpeed;
    public Transform spawnPoint;
    public GameObject projectile;
    public float timeBetweenCasts;
    [HideInInspector] public Vector3 direction;

    private PieMenu pieMenuScript;
    private Vector3 mousePosition;
    private float lastTimeStamp;

    private void Start()
    {
        pieMenuScript = FindObjectOfType<PieMenu>();
        lastTimeStamp = Time.time - timeBetweenCasts;
    }


    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && Time.time - lastTimeStamp >= timeBetweenCasts && pieMenuScript.pieSelection == PieMenu.spellMenuSelection.None)
        {
            //reset the timer
            lastTimeStamp = Time.time;

            //TODO: Object pooling

            RaycastOnMouseClick();

            //ignore collider between the character and the projectile.
            Physics.IgnoreCollision(GetComponentInParent<CapsuleCollider>(), projectile.GetComponent<Collider>());
        }
            
    }

   private void RaycastOnMouseClick()
    {
    
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit groundHit;
        Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 2);
        if (Physics.Raycast(ray, out groundHit, 300f))
        {
            mousePosition = groundHit.point;
            direction = (mousePosition - spawnPoint.transform.position).normalized;
            Debug.Log(direction);
            Shoot(groundHit);
        }
    }
        
    void Shoot(RaycastHit hit)
    {
            var projectileInstance = Instantiate(projectile, spawnPoint.transform.position, Quaternion.identity).GetComponent<ShootProjectile>();   
            
    }

    
}


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

public class ShootProjectile : MonoBehaviour {

    Vector3 shootDirection;
    private float speed;
    private TestSpell testSpellScript;

    private void Update()
    {
        this.transform.Translate(shootDirection * Time.deltaTime * speed);
    }

    private void Awake()
    {
        testSpellScript = FindObjectOfType<TestSpell>();
        shootDirection = testSpellScript.direction;
        speed = testSpellScript.projectileSpeed;
    }

}

When you hit the ground with the raycast add the height that you want it to have:

hit.position + Vector3.up * 2;

That will get the hit position and add 2 units in the world up direction. I don’t know how your scene is oriented, but you can change the direction vector based on your case