Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Timurov · Dec 20, 2012 at 11:37 AM · c#physicsdirectionspherecast

How to get the Distance, Collision and Direction for each game object in my list?

So, I've a list with a game objects and I'm very uncertan about how to get the distance, collistion, direction for each of them. It is sutable to use the SphereCast, to cover my needs, becouse I know it is only class functions that can do it for me. I wrote the code to accomplish the task, but its grabbing the data just for 1 game object in my list, insted of all of them, I wish to grab data from all game object in my list. Any ideas to change the code or approaches?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class example : MonoBehaviour {
    
     RaycastHit hit;
     
     Vector3 direction;
         
     public static  List<float> sphereCast = new List<float>();
     
     void Update() {
         CharacterController charCtrl = GetComponent<CharacterController>();
         foreach(GameObject g in xmlreaderUnityObject.unityGameObjectsToCDP)
         {
             Vector3 p1 = g.transform.position;
             if (Physics.SphereCast(p1, charCtrl.height / 2, g.transform.forward, out hit,1))    
             { 
                 float distance = 0;
                 float angleBetween = 0;
                 float contact = 0; 
                 if(hit.collider.enabled)
                 {
                     direction = hit.transform.position - p1;
                     angleBetween = Vector3.Angle(g.transform.forward, direction);
                     contact = 1;
                     distance =  hit.distance;
                 }
                 print("Distance: "+ distance + " Collision: " + contact + " Direction: " + angleBetween + " hit point: "
                         +hit.point + " player position: " + p1);
                     
                 sphereCast.Add(contact);
                 sphereCast.Add(distance);
                 sphereCast.Add(angleBetween);
             }
         }
         
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lighting · Dec 21, 2012 at 12:55 AM

Please use Physics.OverlapSphere() instead, because it returns array of colliders instead of only one argument (RaycastHit) returned by Physics.SphereCast. Please refer to http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Timurov · Dec 21, 2012 at 09:46 AM 0
Share

the Physics.OverlapSphere just returns an array with all colliders touching or inside the sphere. But, I need to return distance, collistion, direction for each of game objects in my array.

avatar image Timurov · Dec 21, 2012 at 09:53 AM 0
Share

that is why I used the Physics.SphereCast

avatar image
0

Answer by SkyElemental · Nov 12, 2021 at 06:15 PM

[code=CSharp] //Just assign these references however you need to. Either at run time, or in the inspector, or whatever suits your needs. //Use an angle_range of 2 - 30 for best results. A value of 2 being directly in front of the entity. Increasing this value //increases the entity's peripheral vision. Using 0 is not recommended. The value will most likely never equal 0.

 Transform entity;
 Vector3 object_position;
 [Range(2, 30)]
 float angle_range = 30f;
 [Range (0.5f, 5f]
 float min_allowed_distance = 3f;
 
 float angle = Vector3.Angle(entity.forward, object_position - entity.position);
 //Debug.Log("Object Angle: " + angle);
 if (Mathf.Abs(angle) < angle_range)
 {
      print("Object in front of the entity: " + angle);
       if (distance < min_allowed_distance)
       {
            //Execute your next commands/methods here.
       }
 }

[/code]

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spherecast Arguments 1 Answer

How to get direction using Physics.SphereCast ? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dynamic parenting to moving platform so object won't slide from it on it own doesn't work properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges