Search code examples
c#unity-game-enginegame-engineprojectile

Unity 3D- The "Is Trigger" Under Sphere Collider Is not allowing my fireball/projectile to fly


I am reading this book about Unity (dated 2018) and it instructed me to click on that "Is Trigger?" box under the Sphere Collider section of the Inspector.

When I don't click it, the projectile flies just fine. But when I click it, it is just stuck at the starting point of the game object.

Here is the projectile code:

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

public class Fireball : MonoBehaviour
{
    public float speed = 10.0f;
    public int damage = 1;
    // Start is called before the first frame update
    
    // Update is called once per frame
    void Update()
    {
        transform.Translate(0, 0, speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider other)
    {
        PlayerCharacter player = other.GetComponent<PlayerCharacter>();
        if (player != null)
        {
            player.Hurt(damage);
        }
        Destroy(this.gameObject);
    }

}

Here is the accompanied code attached to the enemy:


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

public class WanderingAI : MonoBehaviour
{
    public float speed = 3.0f;
    public float obstacleRange = 5.0f;
    private bool _alive;

    [SerializeField] private GameObject fireballPrefab;
    private GameObject _fireball;

    void Start()
    {
        _alive = true;    
    }

    // Update is called once per frame
    void Update()
    {
        if (_alive)
        {
            transform.Translate(0, 0, speed * Time.deltaTime);

            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;
            if (Physics.SphereCast(ray, 0.75f, out hit))
            {
                GameObject hitObject = hit.transform.gameObject;
                if (hitObject.GetComponent<PlayerCharacter>())
                {
                    if (_fireball == null)
                    {
                        _fireball = Instantiate(fireballPrefab) as GameObject;
                        _fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);
                        _fireball.transform.rotation = transform.rotation;
                    }
                }

                else if (hit.distance < obstacleRange)
                {
                    float angle = Random.Range(-110, 110);
                    transform.Rotate(0, angle, 0);
                }
            }
        }
    }

    public void SetAlive(bool alive)
    {
        _alive = alive;
    }
}

And I need to click that "is trigger?" box, otherwise my projectile would just lie on the floor and not doing anything after it hits a wall.

I don't think the problem is with the code itself but with newer updates to Unity after 2018 that I am not familiar with (since I basically just started using Unity like two weeks ago).

I am guessing that I should create the trigger on the C# Script itself and not rely on that check box.

Can anyone verify this and help me out how to apply that code?

Thanks in advance.


Solution

  • Please refer to Colliders -> Collision Action Matrix

    enter image description here

    => If none of your objects is a trigger then OnTriggerEnter is never called and you might rather want to go for OnCollisionEnter


    Also in general as soon as you are dealing with physics (Rigidbody, Rigidbody2D) you do not want to transform your objects using the Transform component!

    Instead of

    void Update()
    {
        transform.Translate(0, 0, speed * Time.deltaTime);
    }
    

    you should rather do

    [SerializeField] private Rigidbody _rigidbody;
    
    private void Awake()
    {
        if(!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
    
        _rigidbody.velocity = _rigidbody.rotation * Vector3.forward * speed;
    }
    

    and let the physics engine handle the movement itself.