raycast not casting.

This is the code.
nothing happens when the ray is hitting the enemy.
i’m trying to make it spawn a new enemy but once i added the code it stopped working with no errors in unity or in visual studios.

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

public class Gun : MonoBehaviour
{
    public Transform BulletPos;
    public Animator GunAnimController;
    public GameObject prefab;
    private GameObject Clone;
    public GameObject EnemyPrefab;


    private void Start()
    {
       
       
    }

    // Update is called once per frame

    public AudioSource GunSound;
    public float timeBetweenShots;
    private float timestamp = 0f;
    public Transform RandomEnemyPos;
    private Vector3 RandomEnemyLoc;
    void Update()
    {



        if (timestamp < Time.deltaTime && (Input.GetButtonDown("Fire1")))
        {
            GunAnimController.Play("GunAnimation");
            RaycastHit TheHit;
            if (Physics.Raycast(BulletPos.position, BulletPos.TransformDirection(Vector3.forward), out TheHit))
            {

                Debug.Log("hit");
                if (TheHit.collider.gameObject.tag == "Enemy")
                {
                    Destroy(TheHit.collider.gameObject);
                    RandomEnemyLoc = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
                    RandomEnemyPos.position = RandomEnemyLoc;
                    Instantiate(EnemyPrefab, RandomEnemyPos);
                }
                Clone = Instantiate(prefab, (TheHit.point), Quaternion.identity);
                Destroy(Clone, 10);

            }
            GunSound.Play();
            timestamp = Time.time + timeBetweenShots;
           





        }
       
   
   
    }
}

Hi @GoEasyOnThePepsi
“nothing happens when the ray is hitting the enemy.”

So your Debug.Log runs and prints “hit” when you do a raycast?

And did you check your enemy GameObject tag?

And are you sure you are not hitting the source object when raycasting? I don’t see you using LayerMask.

Add more Debug.Logs, see if you actually hit correct objects (= print hit GameObject name).

You could also use Debug.DrawRay / DrawLine to verify your ray goes where you think it should go.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

if (timestamp < Time.deltaTime ...

This is probably the problem ( at least one problem). It’s saying, if the timestamp is less than a really small number, which will never be true, or maybe true on the first frame. What you’ll want is if (Time.time > timeStamp … methinks.

as a style note, you should not call it timeStamp, rather something like “timeWhenNextShotIsReady” as timeStamp does not describe what it’s being used for. In that case it would read…

if (Time.time > timeWhenNextShotIsReady …

which will make it obvious what is intended. A nice little example about how verbose naming conventions can help prevent bugs :smile:

Hope this helps!

1 Like