Daniel Lip
Daniel Lip

Reputation: 11335

How can use one raycast and to hit multiple gameobjects?

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

public class OnMouseOverEvent : MonoBehaviour
{
    public GameObject[] objects;

    private Vector3[] originalpos;

    private void Start()
    {
        originalpos = new Vector3[objects.Length];
        for (int i = 0; i < objects.Length; i++)
        {
            originalpos[i] = objects[i].transform.position;
        }
    }

    private void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.transform.tag == "Test")
            {
               // if (transform.position.z != originalpos.z - 3)
                 //   StartCoroutine(moveToX(transform, new Vector3(transform.position.x, transform.position.y, transform.position.z - 3), 0.1f));
            }
            else
            {
               // StartCoroutine(moveToX(transform, originalpos, 0.1f));
            }
        }
        else
        {
            // reset
           // StartCoroutine(moveToX(transform, originalpos, 0.1f));
        }
    }

    bool isMoving = false;
    IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
    {
        //Make sure there is only one instance of this function running
        if (isMoving)
        {
            yield break; ///exit if this is still running
        }
        isMoving = true;

        float counter = 0;

        //Get the current position of the object to be moved
        Vector3 startPos = fromPosition.position;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
            yield return null;
        }

        isMoving = false;
    }
}

The script was working fine when objects and originalpos were singles. But now I made them arrays since I have more then one gameobject.

I have 3 gameobjects tagged : "Test" , "Test1" , "Test2" I want to perform them same action but for each object hit.

If hitting "Test" move only "Test" on z - 3 and then back to it's original pos. If hitting "Test1" move only "Test1" on z - 3 and then back to it's original pos. And same for "Test2".

Make the same action but only for the hitting object.

Upvotes: 0

Views: 7707

Answers (1)

WQYeo
WQYeo

Reputation: 4071

You can use Physics.RaycastAll, it returns RaycastHit[] which you can loop through.

Like so:

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

RaycastHit[] hits = Physics.RaycastAll(ray, 100f);

// For each object that the raycast hits.
foreach (RaycastHit hit in hits) {

    if (hit.collider.CompareTag("Test")) {
        // Do something.
    } else if (hit.collider.CompareTag("Test1")) {
        // Do something.
    } else if (hit.collider.CompareTag("Test2")) {
        // Do something.
    }
}

Upvotes: 6

Related Questions