Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Other Collider isn't detected by Physics.Linecast

Discussion in 'Physics' started by Deleted User, Nov 15, 2022.

  1. Deleted User

    Deleted User

    Guest

    I'm currently working on a 3D, tile-based puzzle game. In this game the player controls a cube object and the animation it must use when moving is decided by a selectable object. One of these selectable objects is a teleporter and it's the one that's giving me trouble

    The code that decides the cube's animation is this one:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. public class TeleporterCube_SOLoading : CubeSOLoading
    4. {
    5.     public override void SetCubeTransformationSO()
    6.     {
    7.         for (int index = 0; cubeTransformationSO == null && index <= teleporterMaster.teleporter_Object_DetectionInstances.Count - 1; index++)
    8.         {
    9.             TeleporterObjectDetection teleporterObjectDetection = teleporterMaster.teleporter_Object_DetectionInstances[index];
    10.             teleporterObjectDetection.GetCubeTransformationSO();
    11.         }
    12.     }
    13. }
    The teleporter is a parent object and, among their children, there are tiles. TeleporterCube_SOLoading calls GetCubeTransformationSO() on each teleporter tile until it gets an animation SO (Scriptable Object). The code goes as follows:

    Code (CSharp):
    1. using UnityEngine;
    2. public class TeleporterObjectDetection : ObjectDetection
    3. {
    4.     TeleporterMaster teleporterMaster;
    5.     [Header("Z Axis")]
    6.     public MovementDirection zMovementDirection;
    7.     public Vector3 detectionRayOriginZ;
    8.     public CubeTransformationSO zTransformationSO;
    9.     public GameObject adjacentObjectZ;
    10.     [Header("X Axis")]
    11.     public MovementDirection xMovementDirection;
    12.     public Vector3 detectionRayOriginX;
    13.     public CubeTransformationSO xTransformationSO;
    14.     public GameObject adjacentObjectX;
    15.     void Start()
    16.     {
    17.         teleporterMaster = FindObjectOfType<TeleporterMaster>();
    18.     }
    19.     ...
    20.     ...
    21.     public void GetCubeTransformationSO()
    22.     {
    23.         if (adjacentObjectZ != null || adjacentObjectX != null)
    24.         {
    25.             Vector3 detectionLineEnd = Vector3.zero;
    26.             if (adjacentObjectZ != null)
    27.             {
    28.                 detectionLineEnd = adjacentObjectZ.transform.position + Vector3.up;
    29.                 if (Physics.Linecast(adjacentObjectZ.transform.position, detectionLineEnd, detectableLayers) == true)
    30.                 {
    31.                     teleporterMaster.teleporterCube_SOLoading.cubeTransformationSO = zTransformationSO;
    32.                 }
    33.             }
    34.             else if (adjacentObjectX != null)
    35.             {
    36.                 detectionLineEnd = adjacentObjectX.transform.position + Vector3.up;
    37.                 if (Physics.Linecast(adjacentObjectX.transform.position, detectionLineEnd, detectableLayers) == true)
    38.                 {
    39.                     teleporterMaster.teleporterCube_SOLoading.cubeTransformationSO = xTransformationSO;
    40.                 }
    41.             }
    42.         }
    43.     }
    44. }
    In the scene the cube is next to the teleporter. In this case, TeleporterObjectDetection.adjacentObjectZ is the tile on which the cube stands and it's always detected, so all that's left is to cast a line from there and detect the cube's collider.

    Default scene view.png

    However, the cube's collider is never detected. The cube has a collider component and it's enabled, I've also made sure that it's not a trigger. At first I thought that the linecast was missing it, so I used Debug.DrawRay to see the detection line, it clearly goes through it. I also stopped using a layer mask but it won't detect it either, RaycastHit will always get a null collider too.

    It's true that the cube's gameobject is disabled and it's activated later but I've made sure that GetCubeTransformationSO() is only called once the cube has finished setting up. There aren't any colliders between the cube's collider and the linecast

    I don't know if this is related but I've found that GetCubeTransformationSO().detectionLine is not highlighted in Visual Studio, yet the code actually uses it? I've run the debugger and there's clearly a new value

    detectionLineEnd is not highlighted.PNG

    I really don't know what I'm missing, any suggestions?
     
    Last edited by a moderator: Nov 15, 2022
  2. Deleted User

    Deleted User

    Guest

    Okay, I've fixed it.

    Turns out that the cube couldn't be detected because the cube's mesh was clipping thorugh the tile. I've simply moved the cube higher and it's now detectable

    I don't understand why this is an issue, the colliders aren't passing through each other. It works anyway, so I'll take it
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,521