- Home /
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);
}
}
}
}
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
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.
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]
Your answer
Follow this Question
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
Add force problem 1 Answer