/// <summary> /// Configure this component to <paramref name="parent"/> and configures /// the wheel properties given shapes and meshes. /// </summary> /// <param name="parent">Parent game object, will become TrackWheel.Frame.Parent.</param> /// <param name="positionAlongRotationAxis">Position offset along the rotation axis. Default: 0.</param> /// <param name="nameModelPredicate">Match name vs. model - true if name match the model, otherwise false.</param> /// <returns>TrackWheel component if added, otherwise null (throws if <paramref name="parent"/> already has a TrackWheel component)</returns> public TrackWheel Configure(GameObject parent, float positionAlongRotationAxis = 0.0f, Func <string, TrackWheelModel, bool> nameModelPredicate = null) { // What if we don't have a rigid body in hierarchy? Initialize will fail. m_rb = parent.GetComponentInParent <RigidBody>(); m_frame.SetParent(parent); Radius = Tire.FindRadius(parent, true); // Up is z. var upVector = parent.transform.parent != null? parent.transform.parent.TransformDirection(Vector3.up) : Vector3.up; var rotationAxis = Tire.FindRotationAxisWorld(parent); m_frame.Rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis); m_frame.Rotation *= Quaternion.Euler(0, Vector3.Angle(m_frame.Rotation * Vector3.forward, upVector), 0); m_frame.LocalPosition = positionAlongRotationAxis * Vector3.up; var model = TrackWheelModel.Roller; if (TryFindModel(parent.name, ref model, nameModelPredicate) || TryFindModel(RigidBody?.name, ref model, nameModelPredicate)) { Model = model; } else { Model = TrackWheelModel.Roller; } return(this); }
private void Reset() { if (RigidBody == null) { Debug.LogError("Component: TrackWheel requires RigidBody component.", this); } else { Radius = Tire.FindRadius(RigidBody, true); m_frame.SetParent(RigidBody.gameObject); // Up is z. var upVector = RigidBody.transform.parent != null? RigidBody.transform.parent.TransformDirection(Vector3.up) : Vector3.up; var rotationAxis = Tire.FindRotationAxisWorld(RigidBody); m_frame.Rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis); m_frame.Rotation *= Quaternion.Euler(0, Vector3.Angle(m_frame.Rotation * Vector3.forward, upVector), 0); // This should be rotation axis anchor point. m_frame.LocalPosition = Vector3.zero; if (name.ToLower().Contains("sprocket")) { Model = TrackWheelModel.Sprocket; } else if (name.ToLower().Contains("idler")) { Model = TrackWheelModel.Idler; } else { Model = TrackWheelModel.Roller; } } }