Closed AidinD closed 5 years ago
Hi @AidinD, is your RayPercieve inside the object? You might be able to increase the startOffset when calling Perceive to get out of the object.
Hi @ervteng Thank you for the response.
@awjuliani are you familiar with the collider for 2D RayPercieve?
As for 3. I believe that is the case for the 3D case. But not sure why this would be different for 2D.
There should be no difference between the two in terms of how they do or don't ignore objects. Are you sure that the ray begins within the object that needs to be ignored?
@awjuliani I did a quick setup and took a few screenshots to show my setup and highlight the issue. Here is the agent code and this is the only script used in the project except for an empty Academy script:
using System.Collections; using System.Collections.Generic; using MLAgents; using UnityEngine;
public class Circle : Agent {
private RayPerception2D rayPerception;
public override void InitializeAgent () {
rayPerception = GetComponent<RayPerception2D> ();
}
public override void AgentReset () {
}
public override void CollectObservations () {
float rayDistance = 5f;
float[] rayAngles = { 0f };
string[] detectableObjects = { "wall" };
var rayValues = rayPerception.Perceive (rayDistance, rayAngles, detectableObjects);
Debug.Log (rayValues[0]);
Debug.Log (rayValues[1]);
Debug.Log (rayValues[2]);
AddVectorObs (rayValues);
}
public override void AgentAction (float[] vectorAction, string textAction) {
}
}
The first screenshot shows a non working scenario. You can see in the console output that all the values that are returned from the rayPerception2D are zeroes. Note that the agent is untagged while the Wall is tagged as "wall" and both are inside the "Default" layer.
In this second screenshot you can see that we actually get non zero values back from the rayPerception2D. The only difference now is that the agent is put inside the "Ignore Raycast" layer.
I am having the same issue, I've been trying to figure out why my machine learning samples do not work until I found out the same problem and wanted to report.
It appears that the different physics systems handle raycasts which start inside a collider differently:
For 2D: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.
For 3D: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Note: Raycasts will not detect Colliders for which the Raycast origin is inside the Collider.
This is a property of the physics systems, so there's not much that the RayPerception code can do about it right now.
If you got into Edit -> Project Settings -> Physics2D, uncheck the Queries start in colliders
checkbox and see if that helps.
Thank you very much. I have totally missed this setting! I will close this now.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Describe the bug A clear and concise description of what the bug is.
To Reproduce Steps to reproduce the behavior:
Expected behaviour: The float distance should show the distance to the tagged objects included in detectable objects if in range
Actual behaviour: The distance is always 0 since it hits its own collider
It works if changing the layer to "Ignore "Raycast" layer.