1

それで、私はゲームオブジェクトのリストを持っていますが、それぞれの距離、衝突、方向を取得する方法について非常に確信が持てません。私のニーズをカバーするために 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);
            }
        }

    }
}
4

1 に答える 1

0

その理由は、1 つの GameObject しかヒットしないためです。ヒットしたすべてのゲームオブジェクトと、それを送信したゲームオブジェクトを表示するシーンをセットアップしました。 プロジェクト例

于 2012-12-30T03:06:22.087 に答える