9/9/2023 0 Comments Unity physics raycast![]() So it should instead compare the hit's gameObject to the target's gameObject, ignoring the tag, since a correct tag alone isn't enough. So, you could rename and change your SortObjects() function to check for both conditions right in the loop ( InFront(target) & HasLineOfSight(objectTag)), and filter the objects out right in there, since only those objects are of interest.Īlso, your HasLineOfSight() method checks the tag of the hit object, but what you probably wanted to do, is to check if the raycast actually hits that exact object. And other tagged objects that are actually visible, are not picked up this way. That object might be obstructed by something else, so the method returns false. SortObjects() determines the closest tagged object, and then you only handle that single object in ObjectCheck(). In your script, only the closest object to the main camera gets checked. I'm essentially calling ObjectCheck() with the tag I want to check for to get the closest, visible, object with that tag. If (InFront(target) & HasLineOfSight(objectTag)) Target = SortObjects(objectTag).transform Public static bool ObjectCheck(string objectTag) ![]() If (Vector3.Distance(, thisObject.position) <= Vector3.Distance(, thisObject.position)) If (Vector3.Distance(thisObject.position, TargetName = GameObject.FindGameObjectsWithTag(objectTag) įor (int i = 0 i < targetName.Length i++) Public static GameObject SortObjects(string objectTag) If (Physics.Raycast(thisObject.position, direction, out hit, length))ĭebug.DrawRay(thisObject.position, direction * 0.96f, Color.red) Vector3 direction = target.position - thisObject.position Public static bool HasLineOfSight(string objectTag) ![]() Debug.DrawLine(transform.position, target.position, een) Vector3 directionToTarget = thisObject.position - target1.position įloat angleOnXAxis = Vector3.Angle(thisObject.right, directionToTarget) įloat angleOnYAxis = Vector3.Angle(thisObject.up, directionToTarget) Public static bool InFront(Transform target1) target = GameObject.FindGameObjectWithTag("Trees").transform ThisObject = GameObject.Find("Main Camera").GetComponent() Start is called before the first frame update My problem is, this seems to work intermittently! Sometimes it finds the tagged objects, other times it will be right in front of the view yet it will miss it completely! Can anyone advise about what I'm doing wrong? public static Transform target Okay so I'm making a photography game where when you 'take a photo', Unity sends a few raycasts forward to check if certain tagged items are in the photo (all within the cameras FOV).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |