public void TestStaticPerceiveNoTags() { SetupScene(); var obj = new GameObject("agent"); var perception = obj.AddComponent <RayPerceptionSensorComponent3D>(); perception.RaysPerDirection = 0; // single ray perception.MaxRayDegrees = 45; perception.RayLength = 20; perception.DetectableTags = null; var radii = new[] { 0f, .5f }; foreach (var castRadius in radii) { perception.SphereCastRadius = castRadius; var castInput = perception.GetRayPerceptionInput(); var castOutput = RayPerceptionSensor.Perceive(castInput); Assert.AreEqual(1, castOutput.RayOutputs.Length); // Expected to hit the cube Assert.AreEqual("cube", castOutput.RayOutputs[0].HitGameObject.name); Assert.AreEqual(-1, castOutput.RayOutputs[0].HitTagIndex); } }
public void TestStaticPerceiveInvalidTags() { SetupScene(); var obj = new GameObject("agent"); var perception = obj.AddComponent <RayPerceptionSensorComponent3D>(); perception.RaysPerDirection = 0; // single ray perception.MaxRayDegrees = 45; perception.RayLength = 20; perception.DetectableTags = new List <string>(); perception.DetectableTags.Add("Bad tag"); perception.DetectableTags.Add(null); perception.DetectableTags.Add(""); perception.DetectableTags.Add(k_CubeTag); var radii = new[] { 0f, .5f }; foreach (var castRadius in radii) { perception.SphereCastRadius = castRadius; var castInput = perception.GetRayPerceptionInput(); // There's no clean way that I can find to check for a defined tag without // logging an error. LogAssert.Expect(LogType.Error, "Tag: Bad tag is not defined."); var castOutput = RayPerceptionSensor.Perceive(castInput); Assert.AreEqual(1, castOutput.RayOutputs.Length); // Expected to hit the cube Assert.AreEqual("cube", castOutput.RayOutputs[0].HitGameObject.name); Assert.AreEqual(3, castOutput.RayOutputs[0].HitTagIndex); } }
public void GetSensorStuff() { // this is a GetComponent<> in the real code // RayPerceptionSensorComponent attached to child gameobject of agent gameobject // This object is rotated to move sensor around RayPerceptionSensorComponent3D rayComponent = new RayPerceptionSensorComponent3D(); var lengthOfRayOutputs = RayPerceptionSensor .Perceive(rayComponent.GetRayPerceptionInput()) .RayOutputs .Length; var rayDistances = new float[5]; List <float[]> rayBuffers = new List <float[]>() { new float[(2 + 2) * lengthOfRayOutputs], new float[(2 + 2) * lengthOfRayOutputs], new float[(2 + 2) * lengthOfRayOutputs], new float[(2 + 2) * lengthOfRayOutputs], new float[(2 + 2) * lengthOfRayOutputs] }; var rayOutputs = RayPerceptionSensor .Perceive(rayComponent.GetRayPerceptionInput()) .RayOutputs; for (int i = 0; i < 5; i++) { rayOutputs[i].ToFloatArray(2, 0, rayBuffers[i]); } // add just the distances to a new float array which represents all the ray cast distances var distances1 = rayBuffers[0][3]; var distances2 = rayBuffers[1][3]; var distances3 = rayBuffers[2][3]; var distances4 = rayBuffers[3][3]; var distances5 = rayBuffers[4][3]; // I want to convert these distances into Vector3's // assuming this script is attached to gameobject the rays are cast from //RayCastHitLocation( // transform.rotation, // transform.position, // distances1.ReverseNormalise(rayComponent.RayLength) //); }
/// <summary> /// Sets up arrays which can be used to store vector information from the ray perception sensor /// </summary> private void SetUpRayBuffers() { var lengthOfRayOutPuts = RayPerceptionSensor .Perceive(_eyes.GetRayPerceptionInput()) .RayOutputs .Length; _rayBuffers = new List <float[]>() { new float[(2 + 2) * lengthOfRayOutPuts], new float[(2 + 2) * lengthOfRayOutPuts], new float[(2 + 2) * lengthOfRayOutPuts], new float[(2 + 2) * lengthOfRayOutPuts], new float[(2 + 2) * lengthOfRayOutPuts] }; }
public List<float> GetSensorData() { // check for the sensor data RayPerceptionInput input = distanceSensorComponent.GetRayPerceptionInput(); RayPerceptionOutput output = RayPerceptionSensor.Perceive(input); var list = output.RayOutputs; //RayPerceptionOutput.RayOutput output0 = (RayPerceptionOutput.RayOutput)list.GetValue(0); //print(output0.HitFraction); List<float> hitFractions = new List<float>(); foreach (var listItem in list) { RayPerceptionOutput.RayOutput outputRef = (RayPerceptionOutput.RayOutput)listItem; hitFractions.Add(outputRef.HitFraction); } return hitFractions; }
void Update() { if (Input.GetKeyDown(KeyCode.R)) { ReSetPosition(); } if (Input.GetKeyDown(KeyCode.P)) { // 레이퍼셉션의 각 태그번호를 가져오는 코드 r3 = RayPerceptionSensor.Perceive(r2); for (int i = 0; i < r3.RayOutputs.Length; i++) { print(r3.RayOutputs[i].HitTagIndex + ", " + i + "번째"); } } }
public override void OnActionReceived(float[] vectorAction) { // 만일 vectorAction의 0번 값이 0이면 왼쪽으로 회전하고, 1이면 회전하지 않고, 2면 오른쪽으로 회전하게 하고 싶다. float rotvalue = vectorAction[0] - 1; rotY += rotvalue * rotSpeed; transform.localEulerAngles = new Vector3(0, rotY, 0); // 만일 vectorAction의 1번 값이 0이면 가만히 있고, 1이면 앞으로 움직인다. Vector3 dir = transform.forward * vectorAction[1]; transform.position += dir * moveSpeed * Time.deltaTime; //가만히 있을 때 받는 벌점 if (transform.position == previousPos) { AddReward(-1.0f / gm.CustomMaxStep); } // 탈출 시간에 따른 벌점 AddReward(-1.0f / gm.CustomMaxStep); // 불이랑 가까워 졌을 때 받는 벌점 float nowFireDisFromPlayer = Vector3.Distance(transform.position, transform.position); float preFiredisFromPlayer = Vector3.Distance(transform.position, previousPos); if (preFiredisFromPlayer >= nowFireDisFromPlayer) { AddReward(-10.0f / gm.CustomMaxStep); } if (detectEixt == false) { // 레이퍼셉션의 각 태그번호를 가져오는 코드 r3 = RayPerceptionSensor.Perceive(r2); for (int i = 0; i < r3.RayOutputs.Length; i++) { //print(r3.RayOutputs[i].HitTagIndex + ", " + i + "번째"); // 만약 레이에서 출구가 감지되면 상점 if (r3.RayOutputs[i].HitTagIndex == 1) { AddReward(1.0f); detectEixt = true; break; } } } else { // 레이퍼셉션의 각 태그번호를 가져오는 코드 r3 = RayPerceptionSensor.Perceive(r2); for (int i = 0; i < r3.RayOutputs.Length; i++) { //print(r3.RayOutputs[i].HitTagIndex + ", " + i + "번째"); // 만약 레이에서 출구가 감지되지 않는다면 벌점 if (r3.RayOutputs[i].HitTagIndex == 1) { break; } if (i == r3.RayOutputs.Length) { AddReward(-10.0f); } } } //전프레임 위치 저장 previousPos = transform.position; // 불을 감지했을 때 결정할 함수(네비게이션) //OnDetectFireDecision(); }