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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Raycast is acting strangely

Discussion in 'Scripting' started by Red_Core1, Sep 4, 2023.

  1. Red_Core1


    Jan 27, 2019
    I'm trying to instantiate an object on top of another one. To do this I decided to draw raycast from above the first object and instantiate next object on hit.point. however. for some reason it always instantiates the object on y 0.05. this means the raycast goes through collider and reaches this very specific point.

    here's some other helpful info
    the object we cast a ray on is a child of an empty game object. this object has a cube collider, not set to trigger, no rigidbody. there is no other object located before or after the object raycast needs to hit. There's nothing on 0.05y.

    here's the relevant bit of code
    Code (CSharp):
    1. if (Physics.Raycast(RayPos, transform.TransformDirection(Vector3.down), out hit,
    2.     Mathf.Infinity))
    3. {
    4.     int ChosenFlora = Random.Range(0, biome.Flora.Length);
    5.     print(hit.point);
    6.     Instantiate(biome.Flora[ChosenFlora], hit.point, Quaternion.identity);
    7.     Debug.DrawRay(RayPos, transform.TransformDirection(Vector3.down) * 1000, Color.cyan);
    8.     Debug.LogError("stop");
    9. }
    This piece of code is a part of a procedural world generation project. and it's task is to spawn trees, bushes, and other plants on top of existing tiles. this code is executed for each tile after everything else has been generated
  2. Red_Core1


    Jan 27, 2019
    Small update
    It specifically works with the objects I want this code to work on.
    My theory is that raycast for some reason reads tile's original size which is small. 0.1 to be exact. But doesn't realise that it has been changed previously in the code. I printed the localscale.y of tile before drawing raycast and it is updated there. I'm confused
  3. Red_Core1


    Jan 27, 2019
    That was in fact what was happening. managed to fix the problem by creating coroutine and starting to raycast after the end of frame
  4. wideeyenow_unity


    Oct 7, 2020
    oof, stray away from ever using coroutines, as they wind up making more problems than they solve.

    If you want a simple function of make a cube on the side of a cube hit, enjoy this script: :)
    Code (CSharp):
    1. public class AddCube : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         if (Input.GetMouseButtonDown(0))
    6.         {
    7.             Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
    8.             if (Physics.Raycast(ray,out RaycastHit hit,500f))
    9.             {
    10.                 Vector3 sideVector = new Vector3(
    11.                     hit.transform.localScale.x * hit.normal.x,
    12.                     hit.transform.localScale.y * hit.normal.y,
    13.                     hit.transform.localScale.z * hit.normal.z);
    14.                 Instantiate(hit.transform.gameObject,
    15.                     hit.transform.position + sideVector, hit.transform.rotation);
    16.             }
    17.         }
    18.     }
    19. }