Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

This must be a bug , ya??

Discussion in 'Physics' started by InitusInteractive, Sep 18, 2018.

  1. InitusInteractive

    InitusInteractive

    Joined:
    Aug 16, 2018
    Posts:
    128
    I've been having some issues with SphereCast where at longer distances it doesn't detect object or it will detect the wrong ones.

    Very Simple Steps to Replicate
    1 Create a new scene
    2 attach this script to the camera.
    3 Start Scene and watch the magic..

    What should happen is that the wall should have a hole in the middle and it shouldn't change as it gets closer but something else happens, oh yes it does. :confused:

    The Sphere cast is "forward" and the debug line in scene view proves it's the correct location but the sphere is not being cast in the same direction.

    This is causing problems with a 3D navigation AI.. please help. :eek:


    Here is the script



    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class _casterTest : MonoBehaviour {
    4.  
    5.     private float _range = 250;
    6.     public float Speed = 50f;
    7.  
    8.  
    9.     void Start ()
    10.     {
    11.         InitScene();
    12.         transform.position = new Vector3(10, 10, -_range);
    13.         transform.rotation = Quaternion.identity;
    14.  
    15.         InvokeRepeating("KillBlocks", 0, .01f);
    16.  
    17.     }
    18.  
    19.     void InitScene()
    20.     {
    21.         for(int x=0;x<20;x++)
    22.             for (int y = 0; y < 20; y++)
    23.             {
    24.                 var block = GameObject.CreatePrimitive(PrimitiveType.Cube);
    25.                 block.transform.position = new Vector3(x, y, 0);
    26.  
    27.             }
    28.     }  
    29.  
    30.     void Update () {
    31.  
    32.         float step = Speed * Time.deltaTime;
    33.         transform.position = Vector3.MoveTowards(transform.position, new Vector3(10,10,-10), step);
    34.     }
    35.  
    36.     void KillBlocks()
    37.     {
    38.         var distantpoint = transform.TransformPoint(Vector3.forward * _range);
    39.         Cast(transform.position, distantpoint, Color.black);
    40.  
    41.         Ray cast = new Ray(transform.position, distantpoint);
    42.         RaycastHit rch = new RaycastHit();
    43.  
    44.         var hit = Physics.SphereCast(cast, 3, out rch, _range);
    45.         if(hit)
    46.             Destroy(rch.transform.gameObject);
    47.  
    48.     }
    49.  
    50.     private void Cast(Vector3 start, Vector3 end, Color clr)
    51.     {
    52.         Debug.DrawLine(start, end, clr, .25f);
    53.     }
    54. }
    55.  
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    There's a bug in your code:
    Code (CSharp):
    1. Ray cast = new Ray(transform.position, distantpoint);
    should be:
    Code (CSharp):
    1. Ray cast = new Ray(transform.position, (distantpoint-transform.position).normalized);
    The Ray is constructed out of a position and a direction, not as a start point and an end point, as your code tries.
     
  3. InitusInteractive

    InitusInteractive

    Joined:
    Aug 16, 2018
    Posts:
    128
    Thanks Edy!
     
    Edy likes this.