So, I’ve made a prefab object, and I’ve written a script to make it stick to my ground as I move it in the editor (I want it to be something that only ever appears flat to the ground, so being able to place it in the editor is nice). My code is here:
[CustomEditor(typeof(Pad))]
[CanEditMultipleObjects]
public class PadEditor : Editor
{
void OnSceneGUI()
{
RaycastHit h;
foreach (Transform transform in Selection.transforms)
{
if (transform.gameObject.GetComponent<Pad>() == null) continue;
if (Physics.Raycast(transform.position, -transform.up, out h))
{
if (h.collider.tag == "Ground")
{
transform.rotation = Quaternion.LookRotation(Vector3.Exclude(h.normal, transform.forward), h.normal);
transform.position = h.point + transform.up * 0.05f;
}
}
else
if (Physics.Raycast(transform.position, transform.up, out h))
{
if (h.collider.tag == "Ground")
{
transform.rotation = Quaternion.LookRotation(Vector3.Exclude(h.normal, transform.forward), h.normal);
transform.position = h.point + transform.up * 0.05f;
}
}
}
}
}
This works absolutely perfectly… as long as I only try and move one object at once. If I select more than one, it works for a little bit, but then starts moving all over the place.
The error seen here occurs by moving the objects along the z-axis only. Once they leave the red object, one object moves correctly, and the other seems to hop about at random.
I’m completely stumped by this. I can’t think of anything that would cause this, if anyone else can, then I’d be very grateful if you would give me a hand in working this out. Thanks.