N
N
nano_e_t_42021-05-10 21:43:31
Unity
nano_e_t_4, 2021-05-10 21:43:31

How to ignore BoxCollider?

Hello.
Who knows / encountered how you can ignore colliders when using

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;


I create a cartridge, in this way calculate the point where this cartridge needs to be directed and add velocity to it:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        //check if ray hits something
        Vector3 targetPoint;
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log("gameobject name " + hit.collider.gameObject.name);
            Debug.Log("gameobject point" + hit.point);
            targetPoint = hit.point;
        }   
        else
            targetPoint = ray.GetPoint(75); //Just a point far away from the player
            

        //Calculate direction from attackPoint to targetPoint
        Vector3 directionWithoutSpread = targetPoint - attackPoint.position;

        //Calculate new direction with spread
        Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0); //Just add spread to last direction

        GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity); 
        Debug.Log("directionWithSpread " + directionWithSpread);
        currentBullet.GetComponent<Rigidbody>().velocity = directionWithSpread * shootForce;


and everything works fine, the bullets reach the place where I clicked the mouse. but there are objects in the scene with a big BoxCollider. and when I click on the place where this BoxCollider is defined, the cartridge flies in the wrong direction, judging by the debug, because it determines the “click place” not on the meshRender object, but on the border of the BoxCollider.

Question - how it is possible to overcome such behavior to ignore these collider?

ps
Checking BoxCollider as a trigger unfortunately does not help (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
ReyGrau, 2021-05-11
@nano_e_t_4

Use LayerMask

U
Udee Mo, 2021-05-16
@UdeeMo

Either put your objects with large colliders in the Layer and disable interaction with this layer for bullets, or if you need bullets to hit the model (and not into an empty place that is closed by the collider), then you need to make your own collider from small cubes that will cover your model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question