Search Unity

3d Isometric projectiles are colliding with the ground

Discussion in 'Getting Started' started by Crash-Landon, Jun 23, 2022.

  1. Crash-Landon

    Crash-Landon

    Joined:
    Jun 11, 2021
    Posts:
    1
    I'm working on an isometric prototype and have written the code below to shoot a projectile from the player to the mouse location when the "1" key is pressed. It works, but the projectile collides with my terrain almost immediately unless the mouse is at the edge of the screen. Any thoughts on how I can have projectiles track just above the terrain level?




    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ShootSpell : MonoBehaviour
    7. {
    8.     public Camera cam;
    9.     public GameObject projectile;
    10.     public Transform firePoint;
    11.     public float projectileSpeed = 30.0f;
    12.     private Vector3 destination;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetButtonDown("#1"))
    24.         {
    25.             ShootProjectile();
    26.         }
    27.  
    28.     }
    29.  
    30.     void ShootProjectile()
    31.     {
    32.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    33.         RaycastHit hit;
    34.  
    35.         if (Physics.Raycast(ray, out hit))
    36.             destination = ray.GetPoint(50);
    37.  
    38.         InstantiateProjectile();
    39.     }
    40.  
    41.     void InstantiateProjectile()
    42.     {
    43.         var projectileObj = Instantiate(projectile, firePoint.position, Quaternion.identity) as GameObject;
    44.         projectileObj.GetComponent<Rigidbody>().velocity = (destination - firePoint.position).normalized * projectileSpeed;
    45.     }
    46. }
    47.  
    48.