Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Raycast does not get anything.

Discussion in 'Scripting' started by LeLoicLe, Aug 16, 2020.

  1. LeLoicLe

    LeLoicLe

    Joined:
    May 27, 2020
    Posts:
    25
    HI
    So i tried to get gameobject in the scene using a raycast.
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WaypointController : MonoBehaviour
    6. {
    7.     public GameObject[] neighbours;
    8.     public List<GameObject> viewingNeighbours;
    9.     RaycastHit hitInitialise;
    10.     public List<GameObject> pNeighbours;
    11.     void Start()
    12.     {
    13.         neighbours = GameObject.FindGameObjectsWithTag("Waypoint");
    14.         for (int i = 0; i < neighbours.Length; i++)
    15.         {
    16.             pNeighbours.Add(neighbours[i]);
    17.         }
    18.         for (int i = 0; i < pNeighbours.Count; i++)
    19.         {
    20.             Debug.DrawLine(transform.position, pNeighbours[i].transform.position, Color.blue, 2000);
    21.             if (Physics.Raycast(transform.position, pNeighbours[i].transform.position, out hitInitialise, 2000))
    22.             {
    23.                 viewingNeighbours.Add(hitInitialise.collider.gameObject);
    24.             }
    25.         }  
    26.     }
    27. }
    In the scene (picture) we can clearely see the blue lines going trough the colliders of the other capsules.
    But... The list viewingNeighbourg does not get anything, stills empty.
    I am assuming raycast neither, it deos not get anything neither.
    What did i done wrong?
    Thanks for help. :)
     

    Attached Files:

  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Sorry, I have not tried to replicate this so I'm not certain, but it appears you are using a transform position as the direction of the ray cast. This will only work if your ray origin is at 0,0,0. You probably want to subtract the origin of the ray from the transform position to give you a direction vector from the origin of the ray to the target position. I hope that helps.
     
    LeLoicLe and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Right.
    An alternative, depending on the use case, would be to use a Linecast instead. Unlike a Raycast a Linecast takes two end points of the line you want to check. It's an easy solution when you want to cast a ray between two points.

    Rays, by definition, have a start / origin, a direction and are of infinite length. A raycast can be limited to a certain distance though. A Linecast is always a limited line since it's defined by the two endpoints.
     
    LeLoicLe and AlTheSlacker like this.
  4. LeLoicLe

    LeLoicLe

    Joined:
    May 27, 2020
    Posts:
    25
    So i have done tips you told me, and both are working very good.
    Just notice due to the event case they are not really the same.
    Thank both of you. :)
     
    AlTheSlacker likes this.