I did not manage to make your script work properly in unity 4 yet I found your script very inspiring. I have extended it so that it requires less setup, also did not like to have the antenna stuff sepparated in two scripts so merged all and made it a bit more flexible. BUUUT when finally trying on a vehicle I noticed a glich, and then I tried your script in unity 5.6 and suffers same issue.
This has been solved by sincyn the rotations, I guess it was aplying forcé in the wrong dirs when they where out of sync, added lin in update:
standByTarget.rotation = target.rotation;
You should add this to your script in case you dont whant to use mine otherwise when you rotate the car the antenna goes on it will go nuts.
I changed a bit the names because your two scripts had Target variable referring to different things each.
If you rotate the “move this” object the things goes nuts.–>[SOLVED]
I had prepared this script which in case you select all the bools to true it requires no setup but simply add the script onto an object.
/// https://forum.unity3d.com/threads/car-antenna-physics.464619/
///
/// This script will make the antennas of your vehicle wiggle, by default its all set to auto so you just need to add the script, but if you dissable the autos it lets you define more stuff manually so you can adapt it.
/// If you improve the script please share it in the thread above
public class AntenaWobble : MonoBehaviour {
public bool addRigidBody = true; //automatically add rigidbody to target with default values
public bool findAntenna = true; //automatically find antennaObject, sets as antenna the first ofject in children with a renderer, so have only 1 and the target object.
public bool automaticHelpers = false; // automatically generate neccesary gameobjects for the antenna effect to run, notice that you'll need the root object of the vehicle to not move with the vehicle, if its not your case dissble and generate these manually following the example prefab.
public Transform standByTarget; // when vehicle does not move, this is the target possition target should have.
public Transform target; // The object wich antena will look at, this objet must be outside hyerrchy of the moving element such as car. Can be child of root if root does not move.
public GameObject antenna; // The antenna object you want to wobble, it autoinitialices if "findantenna" is true;
public float Drag =2.5f; // Target Drag
public float SpringForce =80f; //Target Spring
private Rigidbody targetRB ;
private Vector3 LocalDistance; //Distance between the two points
private Vector3 LocalVelocity; //Velocity converted to local space
public Vector3 lookAtAxis = new Vector3 (1,0,0); //Adjust the axis of your antenna object for the look at operation
void Awake(){
//HELPERS
if (automaticHelpers) {
standByTarget = new GameObject("StandByTarget").transform;
standByTarget.SetParent (this.transform);
standByTarget.localPosition= new Vector3(0,1,0);
target = new GameObject("Target").transform;
target.SetParent (this.transform); // temporarily parent
target.localPosition= new Vector3(0,1,0); // need initially i same position as standbytarget
target.SetParent (this.transform.parent); // final parent
}
//RIGIDBODY
if (target.GetComponent<Rigidbody>()) {
targetRB = target.GetComponent<Rigidbody> ();
}
else {
print ("target missing rigidBody, default RB added");
targetRB = target.gameObject.AddComponent<Rigidbody>();
targetRB.mass = 1f;
targetRB.angularDrag = 0f;
targetRB.useGravity = false;
}
targetRB.drag = Drag; // alternativelly you could use the code bellow in fixed update with same ressults.
//ANTENNA
if(findAntenna) antenna = transform.GetComponentInChildren<MeshRenderer>().gameObject;
if (!antenna && !findAntenna) print("antenna must be manually set or enable automatic antenna find");
}
void FixedUpdate () {
//Calculate the distance between the two points
LocalDistance = standByTarget.InverseTransformPoint(target.position);
print("localdistance " + LocalDistance);
targetRB.AddRelativeForce(-(LocalDistance.x)*SpringForce,-(LocalDistance.y)*SpringForce,-(LocalDistance.z)*SpringForce);//Apply Spring
//Calculate the local velocity of the SpringObj point
//LocalVelocity = (target.InverseTransformDirection(targetRB.velocity));
//targetRB.AddRelativeForce(-LocalVelocity.x*Drag,-LocalVelocity.y*Drag,-LocalVelocity.z*Drag);//Apply Drag
}
void Update (){
antenna.transform.LookAt(target.position,lookAtAxis);
standByTarget.rotation = target.rotation;
}
}
In case of manual setup, the object that should have the script is the “move this”
Target is the one with a RB.