Car Antenna physics

Hi there,

Anyone know if its possible to get something like car antenna type physics sim out of the Physics engine?
The spring joint doesn’t really behave that way, as it says in the manual it’s more like an elastic band between two objects. I’ve needed an effect like this often and I’m kinda surprised it doesn’t seem like there is anything implemented.

Any ideas?

Thanks,
Pete

I had the same need in my project but luckily I had an asset already that included exactly what I needed, it was a rubber deformer, all I needed to do was set the vertex colors for my aerials to define how bendy they were, so less at the base more at the end and it works great.

Hey thanks man, that looks great but I am looking for something that will animate a game object rather than verts on a mesh. I’d like to be able to animate nulls in a rig to get the effect.

P.

In that case just add some spring joints to your object and join them to some kinematic objects in you rig.

Thanks but Unfortunately the spring joint behaves more like a piece of elastic pulling two points together.
I also feel as though it is way more complicated than what I’m looking for.
I messed around with scripting my own a bit in the past, I’ll see if I can find something to repurpose. Just a shame there aren’t any simple pos or rotate springs built in as they would be handy for people.

This is more what I was thinking about - Thanks to my bro for the help (knows his physics) :slight_smile:
3025213--683426--image.gif
I attached the project incase anyone else needs something like this, but I reckon it would be ace if there were more simple physics constraints built in to allow us to easily create these cool effects…

Better link in this post - #15

P.

3025213--683429--image.png

3 Likes

Looks epic!! downloading! been looking for this like for LOOOONG time. Thank you!

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”
3081151--231943--upload_2017-5-23_21-29-7.png
Target is the one with a RB.

1 Like

aparently sometimes Works better with Target object as child of the root, so sometimes it will not work if automatic helpers is on, sure it is a modelling package axis and in editor rotation values issue.

3081284--231949--upload_2017-5-23_23-4-58.png

Stryker antenna relative possiton to parent is :
0,0,0

This is for Stryker wich has the antennas attached to body.

For a t80 tank that has them in the turret I had to set the targed as root childs and also needed to set the RB to interpolate.

3081284--231950--upload_2017-5-23_23-5-22.png

Hey nice one! Glad that came in handy for you and nice work cleaning up the script :sunglasses:
I really think a bunch of little components like that that would be super helpful as a standard part of Unity.

This looks amazing! This is exactly what I’ve been looking for too! I tried it out, but I’m getting compile errors and I know nothing about coding. I’m on Unity 5.6. I’m getting the following errors. Does anyone know how I can get this to compile properly?

“error CS0246: The type or namespace name MonoBehaviour' could not be found. Are you missing UnityEngine’ using directive?”
“error CS0246: The type or namespace name Transform' could not be found. Are you missing UnityEngine’ using directive?”
etc…

Try putting

using UnityEngine;

At the top of the script.
(Just a guess, I’m away from Unity right now)
P.

I support this cause monobehaviour is like the base of eburithing.

I STRONGLY recommend you go trough the basix scripting tutorials first. at least priour you try to build a game or demo or prototype.

Is there a Unity package for this? I’m having issues with the structure, and would like to implement a similar behavior. I tried figuring out what “sincyn the rotation” means, but I assume there’s something lost in translation here - any help in preventing the wobbling issue, would be greatly appreciated (as I love the behavior, but when moving my vehicle it just goes wonky). Either way (a unity package or a code example to add to the “LookAt” or “SpringLocal” classes, would be excellent.

Thanks,

Hey Haddicus!

I think the problem was that we were calculating local distance base a transform space that wasnt rotating with the parent. So syncing the rotation of the target fixed this.

I revised my code a little and converted to C# check that out and see if it works for you a little better.
http://www.peterleary.com/ForumFiles/AntennaSpring-VBaOEbQQVv.unitypackage
I reckon it could be made to work better with rotations but for me this seems to do the job.

Let me know how you go!
Pete

6 Likes

It’s a nice effect, only down side is the lack of collisions. You could add a sphere collider to your invisible rigid bodies which would do an okay job solving for simple geometry like walls but the antenna could still pass through smaller or more complex objects. Also, because the rigid bodies don’t actually stick to the top of the antenna you would get some false or inaccurate collisions.

So not quite universal enough to be considered a truly physical solution, but looks great for anything that fits the limitations!

Thanks Pete! My situation doesn’t require collisions, and to be honest, I wouldn’t expect this to. I’m quite happy this now works with rotations.

3297405--255637--antennaWorking.gif

2 Likes

Nice one man! That looks really fun :slight_smile:

Thanks petey, working like a charm :wink:

Thanks @xesf ! Glad it’s working for you. :slight_smile: