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);
}
}
}
}