unity spherecast

void CheckSpherecast(float radius, float detectableDistance, LayerMask detectableLayer) {
  Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  RaycastHit hitInfo;
  if (Physics.SphereCast(ray, radius, out hitInfo, detectableDistance, detectableLayer)) {
    Debug.Log(hitInfo);
  }
}

4.67
6
Nakiya 110 points

                                             void RenderVolume(Vector3 p1 , Vector3 p2 , float radius , Vector3 dir, float distance )
         {
             if (!shape)
             { // if shape doesn't exist yet, create it
                 shape = GameObject.CreatePrimitive(PrimitiveType.Cube).transform;
                Destroy(shape.GetComponent<Collider>()); // no collider, please!
                 shape.GetComponent<Renderer>().material = mat; // assign the selected material to it
             }
             Vector3 scale ; // calculate desired scale
             float diam  = 2 * radius; // calculate capsule diameter
             scale.x = diam; // width = capsule diameter
             scale.y = Vector3.Distance(p2, p1) + diam; // capsule height
             scale.z = distance + diam; // volume length
             shape.localScale = scale; // set the rectangular volume size
                                       // set volume position and rotation
             shape.position = (p1 + p2 + dir.normalized * distance) / 2;
             shape.rotation = Quaternion.LookRotation(dir, p2 - p1);
             shape.GetComponent<Renderer>().enabled = true; // show it
         }
 
         void HideVolume()
         { // hide the volume
             if (shape) shape.GetComponent<Renderer>().enabled = false;
         }
         private Transform shape ; 
         public float range  = 10; // range of the capsule cast
         private float freeDistance = 0;
         void Update()
         {
                   if (Input.Get$$anonymous$$ey("p"))
             { // while P pressed...
                 RaycastHit hit;
                 CharacterController charContr = GetComponent<CharacterController>();
                 var radius = charContr.radius;
                 // find centers of the top/bottom hemispheres
                 Vector3 p1   = transform.position + charContr.center;
                 var p2 = p1;
                 var h = charContr.height / 2 - radius;
                 p2.y += h;
                 p1.y -= h;
                 // draw CapsuleCast volume:
                 RenderVolume(p1, p2, radius, transform.forward, range);
                 // cast character controller shape range meters forward:
                 if (Physics.CapsuleCast(p1, p2, radius, transform.forward,out hit, range))
                 {
                     // if some obstacle inside range, save its distance 
                     freeDistance = hit.distance;
                 }
                 else {
                     // otherwise shows that the way is clear up to range distance
                     freeDistance = range;
                 }
             }
             if (Input.Get$$anonymous$$eyUp("p"))
             {
                 HideVolume(); // hide volume when P is released
             }
         }

4.67 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
cast sphere unity Spherecast in unity how to use spherecast in unity unity spherecasting unity show spherecast unity circlecast vs spherecast UnityEngine.Physics.SphereCast unity 2d spherecast unity spherecast example unity debug spherecast physics.spherecast example unity visualize spherecast hyperx quadcast s uk unity editor draw spherecasts raycast a sphere unity unity spherecast all visualise spherecast unity draw spherecast unity unity spherecastt physics spherecast how to do a spherecast unity 3D unity display spherecast Sphere cast in unity unity raycast spherecast unity draw a sphere cast unity sphere cast unity draw spherecast unity raycast all sphere custom sphere cast unity how to see a sphere cast unity physics spherecast unity Unity sphere cast explained how the sphere cast works in unity unity3d position sphere on raycast show debug sphere debug draw sphere unity spherecast at position sphere cast unity does physics.spherecast work with character controller unity raycast shapes unity spherecast unity physics.raycast 360 degree raycast unity sphere cast origin unity unity debug overlap circle unity draw sphere spherecast unity SphereCastAll unity debug.draw circle drawing a physics.shperecast unity draw sphere cast unity debug draw circle unity spherical raycast unity physics spherecast enable on dragizmos on code sphere cast collision point spherecast unity Draw spher eunity Unity sphere cast scale unity raycast point radius size unity raycast sphere CheckSphere check ray sphere raycast unity Physics.SphereCast unity3d draw check sphere unity unity ray sphere draw sphere debug unity physics.spherecast unity unity cast sphere unity sphere raycast unity draw sphere in editor physics.spherecast physics.spherecast layermask how to draw a sphere cast with float radius unity how to draw a sphere cast with float radius how to draw sphere in unity spherecast non alloc unity point cast unity spherecast at origin raycast sphere unity ow to see my sphere cast unity unity draw sphere unity spherecast how to see it unity physics.spherecast unity spherecast inside unity spherecast hit draw sphere unity drdraw sphere unity DrawPoint unity unity debug draw sphere unity debug.drawsphere u44 debug sphere draw sphere cast unity with program
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source