Hello,
I have this code that loads all joints that represent wheels and spins their motors to make the “vehicle” moving.
List<Joint2D> wheelsJoints = new List<Joint2D>();
wheelsJoints.AddRange(gameObject.GetComponents<HingeJoint2D>()); wheelsJoints.AddRange(gameObject.GetComponents<WheelJoint2D>());
void DriveVehicleHandler (float speed)
{
foreach (Joint2D joint in wheelsJoints)
{
JointMotor2D motor = new JointMotor2D();
motor.motorSpeed = (vehicle.Heading == VehicleHeading.left) ? speed: -speed;
motor.maxMotorTorque = joint.motor.maxMotorTorque;
joint.motor = motor;
}
}
It obviously doesnt work as Joint2D has no “motor” property.
What is a nice way to make this code work? Is there a nice clean solution? (Other than using only one type of 2D joint)? Im quite new to OOP and polymorphism.
Thank you!