Pascal Serrarens 9717ae0335
All checks were successful
Copy Documentation to webserver / copy-documentation (push) Successful in 24s
Initial breitenberg example
2026-05-07 17:26:53 +02:00

45 lines
1.6 KiB
C#

using UnityEngine;
namespace Breitenberg {
public class Vehicle : MonoBehaviour {
[Header("Sensors")]
public Sensor sensorLeft;
public Sensor sensorRight;
[Header("Motors")]
public float baseSpeed = 0f; // optional forward bias
public float gain = 5f; // motor response strength
public bool crossWiring = false; // false = same-side wiring (excitatory); true = cross
[Header("Physics")]
public Rigidbody rb;
public float turnTorque = 5f; // rotational influence
void FixedUpdate() {
float sL = sensorLeft.output;
float sR = sensorRight.output;
// Wiring: same-side (default) maps sL->left motor, sR->right motor.
// crossWiring = true maps sL->right motor (crossed).
float leftInput = crossWiring ? sR : sL;
float rightInput = crossWiring ? sL : sR;
float leftSpeed = baseSpeed + gain * leftInput;
float rightSpeed = baseSpeed + gain * rightInput;
// Convert differential wheel speeds into forward force and torque.
float forward = (leftSpeed + rightSpeed) * 0.5f;
float rotation = (rightSpeed - leftSpeed) * 0.5f;
// Apply forward force at center
Vector3 force = transform.forward * forward;
rb.AddForce(force, ForceMode.Acceleration);
// Apply torque around Y axis
rb.AddTorque(Vector3.up * rotation * turnTorque, ForceMode.Acceleration);
}
}
}