G
G
GmDvlpr2020-01-08 15:44:31
Unity
GmDvlpr, 2020-01-08 15:44:31

Raycast unity2D does not find the object, what should I do?

I want to create a game where enemies spawn and when you click on them they are destroyed.
top view
I was told that you can use Raycast for this, I tried to use it, but nothing happens when I click on the enemies, although I wrote destroy
Here is the code

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

public class Hitenemy : MonoBehaviour
{
    public GameObject man;
    GameObject enemy;
    public Text score;
    public int count = 0;
    void Start()
    {
        StartCoroutine(Spawn());
    }

    
    

    IEnumerator Spawn()
    {
        while (true)
        {
            man.transform.position = new Vector2(Random.Range(-2.21f, 2.195f), Random.Range(-4.354f, 2.498f));
           enemy= Instantiate(man);
            yield return new WaitForSeconds(1.5f);

            //   Destroy(enemy);
            // yield return new WaitForSeconds(1.5f);
        }
    }
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;     
        
        if (Physics.Raycast(ray, out hit, 1000,LayerMask.GetMask("enemi")))
         {            
           Destroy(enemy);
         }

       
    }

    private void OnMouseDown()
    {
       
        enemy=GameObject.FindWithTag("Player");
        Destroy(enemy);
        count++;
       score.text = count.ToString();
    }
}

the second day I can’t solve this game, although I think that this is a matter of 5 minutes (most likely it is)
I tried to hang a script on the camera, nothing happens, I
created a 3d object, removed the mesh collider from it, but by clicking on any area in the score object increases, but when I click on the enemies, they are not destroyed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2020-01-08
@GmDvlpr

let's try step by step. systematize all your past questions and attempts.
I would advise you to do it from scratch, so that it is "clean and clear" and without regard to past attempts and confusion.
- the main object will be in the scene. hit tester and object creator.
- objects are instantiated even through an infinite loop in the corontina (although, as for me, a timer on Update would be better, but I think it doesn’t matter here)
- hit check on the object - on Update via Raycast
now to trifles.

  • find documentation or examples on the RaycastHit hit forum;
    there inside hit - a lot of information what you hit with the beam and it would be good to add a check that you hit exactly the right object, you never know what else with a layer or tag will be marked by accident.
  • the "cast beam" check itself - you should only do it by clicking / Down / Up with the mouse (look towards Input)
  • for working with 2D colliders, there are separate Physics2D recasts. to get rid of crutches with 3d colliders))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question