【发布时间】:2012-12-20 11:31:16
【问题描述】:
所以,我有一个包含游戏对象的列表,我非常不确定如何获得每个对象的距离、碰撞和方向。使用 SphereCast 来满足我的需求是合适的,因为我知道只有类函数才能为我做这件事。 我编写了代码来完成任务,但它只为列表中的 1 个游戏对象获取数据,在所有这些对象中,我希望从列表中的所有游戏对象中获取数据。有什么改变代码或方法的想法吗?
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);
}
}
}
}
【问题讨论】:
-
您是否尝试设置更大的半径?你为什么使用goto?正如我所看到的,整个开关盒构造是显而易见的。您在日志视图中获得了多少个打印输出,1 个或多于 1 个但始终具有相同的值?
-
每个处于活动状态的游戏对象都有自己的值,处于非活动状态的游戏对象不会发送任何内容,但它们应该
标签: c# unity3d distance collision game-physics