简体   繁体   中英

Raycast does not hit an object

Why this code does not print anything after a touch?

    void Update()
{
    if (Input.touchCount > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
        if (hit)
        {
            print(hit.transform.name);
        }
    }
}

Try this:

if (Input.touchCount > 0)
    {
        RaycastHit2D hit;

        if (Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero, out hit))
        {
            print(hit.transform.name);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM