Использование RayCastAll вместо RayCast - PullRequest
0 голосов
/ 03 июля 2019

У меня есть два игровых объекта: ролик и маркер. Когда я использую физику raycast, она работает, а с raycastall - нет. Мне нужно использовать raycastall, потому что мне нужно перебирать коллайдеры. Когда маркер находится на ролике, нормальный raycast не работает, и мне нужно использовать raycastall.

Этот код работает:

if (Input.touchCount > 0)
{
    // get mouse position in screen space
    // (if touch, gets average of all touches)
    Vector3 screenPos = Input.mousePosition;
    // set a distance from the camera
    screenPos.z = 10.0f;
    // convert mouse position to world space
    Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    Physics.Raycast(cameraRay, out hit);
    if (hit.collider.tag == "Floor")
    {
        transform.position = hit.point;
    }

    /*
    Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
    // get current position of this GameObject
    Vector3 newPos = transform.position;
    // set x position to mouse world-space x position
    newPos.x = worldPos.x;
    newPos.z = worldPos.z-3f;
    // apply new position
    transform.position = newPos;
    */

    if (Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        Destroy(gameObject);
        touchhandler = GameObject.Find("Roller").GetComponent<TouchHandler>();
        touchhandler.markerexists = false;
    }
}

А это не так:

if (Input.touchCount > 0)
{
    // get mouse position in screen space
    // (if touch, gets average of all touches)
    Vector3 screenPos = Input.mousePosition;
    // set a distance from the camera
    screenPos.z = 10.0f;
    // convert mouse position to world space
    Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit[] hits;
    hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), 100.0F);
    while (i < hits.Length)
    {
        RaycastHit hit = hits[i];
        if (hit.collider.tag == "Floor") 
        {
            transform.position = hit.point;
        }

        i++;
    }

    if (Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        Destroy(gameObject);
        touchhandler = GameObject.Find("Roller").GetComponent<TouchHandler>();
        touchhandler.markerexists = false;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...