U
U
Uncle Bogdan2021-07-22 22:48:08
Unity
Uncle Bogdan, 2021-07-22 22:48:08

Why doesn't raycast collide?

I click on the floor and generally anywhere, but it gives out that it didn’t collide

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

public class Player : MonoBehaviour
{
    [SerializeField] Camera _playerCamera;
    [SerializeField] RenderUI _render;

    private Vector2 _screenCenter = new Vector2(Screen.width / 2, Screen.height / 2);

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Mouse0))
        {
            releaseRay();
        }
    }

    private void releaseRay()
    {
        Ray ray = _playerCamera.ScreenPointToRay(_screenCenter);

        RaycastHit hit;

        Physics.Raycast(ray, out hit, 1000f);

        if(hit.collider != null)
        {
            if (hit.collider.GetComponent<Ram>())
            {
                _render.RenderInfo(hit.collider.GetComponent<Ram>());
            }
            else if (hit.collider.GetComponent<Processor>())
            {
                _render.RenderInfo(hit.collider.GetComponent<Processor>());
            }
            else if (hit.collider.GetComponent<Motherboard>())
            {
                _render.RenderInfo(hit.collider.GetComponent<Motherboard>());
            }
        }
        else
        {
            print("не столнулось :(");
        }

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrSlowpoke, 2021-07-23
@motkot

Because bool is returned. You need to put physics.raycast in if and process hit inside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question