Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Projectiles Using Velocity and Trigger Colliders

Discussion in 'Scripting' started by ShamusO, Apr 4, 2020.

  1. ShamusO

    ShamusO

    Joined:
    Jan 12, 2016
    Posts:
    6
    Hello all I have a question on projectile and how to best implement them. Currently, I have a system that uses tags and a script that references them. I have a bolt/laser tagged as "Bolt" and a script called "Health" on all destructible/enemy/health having objects. This script detects collisions and if the collided game object has the tag "bolt" detract 1 from the public variable health. When health is >=0 destroy instantiate an explosion at that location and destroy the game object. This all works fine however when I up the velocity past a certain point for some reason the object cannot die. When I lowered the velocity of the projectile again it worked as normal. I am assuming that maybe the bolt is traveling too fast for the health collision detection in the health script, however, the bolts do still destroy on impact with any object tagged as "enemy". Anyway I am gonna insert the two scripts that matter.

    PS:: Also while I have achieved the desired functionality I am not sure if this is the best way to implement it. Previously I attempted to use the collision on the bolt and subtract two health from whatever it hit, but I was having issues understanding how to get attributes from other objects.

    PSS:: Health script was also supposed to return the current health of game object in the console which doesn't happen. I get a message but it says " _health Unity.Debug:Log.(Object)" I was hoping it would log the float that the variable _health refers to.

    Health script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Health : MonoBehaviour {
    6.  
    7.     public float _health;
    8.     public GameObject _explosion;
    9.  
    10.     void OnTriggerStay(Collider other)
    11.     {
    12.         if (other.tag == "Bolt")
    13.         {
    14.             _health = _health -= 1;
    15.             Debug.Log("_health");
    16.  
    17.  
    18.             if (_health <= 0)
    19.             {
    20.                 Instantiate(_explosion, transform.position, new Quaternion (90,0,0,0) );
    21.                 Destroy(gameObject);
    22.             }
    23.         }
    24.  
    25.     }
    26. }
    Destroy on contact script
    (destroys anything but is used currently to destroy lasers when they hit enemy tagged objects)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyOnContact : MonoBehaviour
    6. {
    7.     public string _TagType;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.tag == _TagType)
    12.         {
    13.              Destroy(gameObject);  
    14.         }
    15.         else
    16.         {
    17.             return;
    18.         }
    19.     }
    20. }
    Mover Script (to mover laser/bolt)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mover : MonoBehaviour
    6. {
    7.     public float _moverSpeed;
    8.     public Rigidbody rb;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.         rb.velocity = new Vector3(0, 0, _moverSpeed);
    15.  
    16.         //both implementations work
    17.  
    18.         //rb.velocity = transform.forward * _moverSpeed;
    19.  
    20.     }
    21. }
     
  2. ShamusO

    ShamusO

    Joined:
    Jan 12, 2016
    Posts:
    6