Search Unity

Raycasts don't start at gameobject's center point.

Discussion in 'Scripting' started by maksymr, Jul 1, 2018.

  1. maksymr

    maksymr

    Joined:
    Jan 31, 2015
    Posts:
    9
    Hello, as in screenshot, why does my raycasts start at bottom point of my gameobject instead of center point?

    upload_2018-7-1_11-36-38.png

    This is my human model's hierarchy: upload_2018-7-1_11-39-28.png

    Script is attached to "Player" GameObject that has centered pivot point: upload_2018-7-1_11-40-19.png

    Code (CSharp):
    1. public class RaycastDirections : MonoBehaviour {
    2.     private const int DIRECTIONS_COUNT = 16;
    3.     private const int MAX_DISTANCE = 10;
    4.  
    5.     public float GetDistanceToGround() {
    6.         var distances = new List<float>();
    7.  
    8.         foreach (var direction in GetSphereDirections(DIRECTIONS_COUNT)) {
    9.             Debug.DrawRay(transform.position, direction, Color.blue);
    10.  
    11.             RaycastHit groundHitInfo;
    12.             if (Physics.Raycast(transform.position, direction, out groundHitInfo, MAX_DISTANCE)) {
    13.                 distances.Add(groundHitInfo.distance);
    14.             }
    15.         }
    16.  
    17.         return distances.Any() ? distances.Min() : MAX_DISTANCE;
    18.     }
    19.  
    20.     private Vector3[] GetSphereDirections(int numDirections) {
    21.         var pts = new Vector3[numDirections];
    22.         var inc = Math.PI * (3 - Math.Sqrt(5));
    23.         var off = 2f / numDirections;
    24.  
    25.         foreach (var k in Enumerable.Range(0, numDirections)) {
    26.             var y = k * off - 1 + (off / 2);
    27.             var r = Math.Sqrt(1 - y * y);
    28.             var phi = k * inc;
    29.             var x = (float) (Math.Cos(phi) * r);
    30.             var z = (float) (Math.Sin(phi) * r);
    31.             pts[k] = new Vector3(x, y, z);
    32.         }
    33.  
    34.         return pts;
    35.     }
    36. }
    Thank you for any help
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    are you sure the pivot is at center?
    try clicking this button to switch between center and actual pivot
    upload_2018-7-1_13-25-46.png
     
    maksymr likes this.
  3. maksymr

    maksymr

    Joined:
    Jan 31, 2015
    Posts:
    9
    Ok you're 100% right, I totally forgot about this switch.
    So it's not at the center. Can I change pivot point in Unity or do I need 3D environment?