Reputation: 575
using UnityEngine; using UnityEditor; [CustomEditor(typeof(Test))] public class TestEditor : Editor { Test test; GameObject cube; GameObject sphere; public override void OnInspectorGUI() { base.OnInspectorGUI(); test = (Test)target; if (GUILayout.Button("Create")) { Create(); RayCastTest(); } if (GUILayout.Button("RayCast Test")) { RayCastTest(); } } void Create() { cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = test.transform.position; sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = test.transform.position + Vector3.right * 5f; } void RayCastTest() { Ray ray; RaycastHit hit; ray = new Ray(test.transform.position, Vector3.right); if (Physics.Raycast(ray, out hit, 1000f)) { GameObject target = hit.collider.gameObject; target.transform.localScale *= 2f; } } }
So, How can I get the RayCastTest() function to work in the first part?
Upvotes: 0
Views: 1562
Reputation: 90823
The raycast is from the Physics
engine. Afaik it only works after one Physics
update since before that the Collider
might not be recalculated yet ->The Physics
doesn't "know" that Collider
yet.
You could try and force it using Physics.Simulate
after your Create
if (GUILayout.Button("Create"))
{
Create();
Physics.Simulate(Time.fixedDeltaTime);
RayCastTest();
}
Additionally you might have to disable the Physics.autoSimulation
.. unfortunately the API isn't so clear on that. If it is the case you might do it like
Physics.autoSimulation = false;
Physics.Simulate(Time.fixedDeltaTime);
Physics.autoSimulation = true;
Also Physics.SyncTransforms
might help
When a
Transform
component changes, anyRigidbody
orCollider
on thatTransform
or its children may need to be repositioned, rotated or scaled depending on the change to theTransform
. Use this function to flush those changes to thePhysics
engine manually.
Since you are moving your created objects you might additionally have to call this (again might be necessary to be used together with Physics.autoSyncTransforms
) before the simulation like
if (GUILayout.Button("Create"))
{
Create();
Physics.SyncTransforms();
Physics.autoSimulation = false;
Physics.Simulate(Time.fixedDeltaTime);
Physics.autoSimulation = true;
RayCastTest();
}
Upvotes: 3