Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Car Antenna physics

Discussion in 'Physics' started by petey, Apr 5, 2017.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    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
     
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    825
    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.
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    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.
     
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    825
    In that case just add some spring joints to your object and join them to some kinematic objects in you rig.
     
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    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.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    This is more what I was thinking about - Thanks to my bro for the help (knows his physics) :)

    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...

    http://www.peterleary.com/ForumFiles/AntennaSpring-71drHyGrqj.unitypackage

    P.
     
    Alex_TNT and TooManySugar like this.
  7. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    853
    Looks epic!! downloading! been looking for this like for LOOOONG time. Thank you!
     
  8. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    853
    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.

    Code (CSharp):
    1. /// https://forum.unity3d.com/threads/car-antenna-physics.464619/
    2. ///
    3. /// 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.
    4.  
    5. /// If you improve the script please share it in the thread above
    6.  
    7.  
    8. public class AntenaWobble : MonoBehaviour {
    9.     public bool addRigidBody = true;    //automatically add rigidbody to target with default values
    10.     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.
    11.     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.
    12.  
    13.     public Transform standByTarget;     // when vehicle does not move, this is the target possition target should have.
    14.     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.
    15.     public GameObject antenna;            // The antenna object you want to wobble, it autoinitialices if "findantenna" is true;
    16.  
    17.     public float Drag =2.5f;                 // Target Drag
    18.     public float SpringForce =80f;        //Target Spring
    19.     private Rigidbody targetRB ;
    20.  
    21.     private Vector3 LocalDistance;    //Distance between the two points
    22.     private Vector3 LocalVelocity;    //Velocity converted to local space
    23.  
    24.     public Vector3 lookAtAxis = new Vector3 (1,0,0);         //Adjust the axis of your antenna object for the look at operation
    25.  
    26.  
    27.     void Awake(){
    28.         //HELPERS
    29.         if (automaticHelpers) {
    30.             standByTarget = new GameObject("StandByTarget").transform;
    31.             standByTarget.SetParent (this.transform);
    32.             standByTarget.localPosition= new Vector3(0,1,0);
    33.    
    34.    
    35.             target = new GameObject("Target").transform;
    36.             target.SetParent (this.transform); // temporarily parent
    37.             target.localPosition= new Vector3(0,1,0); // need initially i same position as standbytarget
    38.        
    39.    target.SetParent (this.transform.parent); // final parent
    40.  
    41.         }
    42.  
    43.         //RIGIDBODY
    44.         if (target.GetComponent<Rigidbody>()) {
    45.             targetRB = target.GetComponent<Rigidbody> ();
    46.         }
    47.         else {
    48.             print ("target missing rigidBody, default RB added");
    49.             targetRB = target.gameObject.AddComponent<Rigidbody>();
    50.             targetRB.mass = 1f;
    51.             targetRB.angularDrag = 0f;  
    52.             targetRB.useGravity = false;
    53.         }
    54.         targetRB.drag = Drag; // alternativelly you could use the code bellow in fixed update with same ressults.
    55.  
    56.         //ANTENNA
    57.         if(findAntenna)        antenna = transform.GetComponentInChildren<MeshRenderer>().gameObject;
    58.         if (!antenna && !findAntenna)  print("antenna must be manually set or enable automatic antenna find");
    59.     }
    60.  
    61.  
    62.     void FixedUpdate () {
    63.         //Calculate the distance between the two points
    64.         LocalDistance = standByTarget.InverseTransformPoint(target.position);
    65.         print("localdistance " + LocalDistance);
    66.         targetRB.AddRelativeForce(-(LocalDistance.x)*SpringForce,-(LocalDistance.y)*SpringForce,-(LocalDistance.z)*SpringForce);//Apply Spring
    67.  
    68.  
    69.         //Calculate the local velocity of the SpringObj point
    70.         //LocalVelocity = (target.InverseTransformDirection(targetRB.velocity));
    71.         //targetRB.AddRelativeForce(-LocalVelocity.x*Drag,-LocalVelocity.y*Drag,-LocalVelocity.z*Drag);//Apply Drag
    72.     }
    73.  
    74.     void Update (){
    75.         antenna.transform.LookAt(target.position,lookAtAxis);
    76. standByTarget.rotation = target.rotation;
    77.     }
    78. }
    In case of manual setup, the object that should have the script is the "move this"
    upload_2017-5-23_21-29-7.png
    Target is the one with a RB.
     
    Last edited: May 24, 2017
  9. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    853
    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.

    upload_2017-5-23_23-3-36.png

    upload_2017-5-23_23-4-40.png

    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.
     

    Attached Files:

    Last edited: May 24, 2017
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    Hey nice one! Glad that came in handy for you and nice work cleaning up the script :cool:
    I really think a bunch of little components like that that would be super helpful as a standard part of Unity.
     
  11. Dobalina

    Dobalina

    Joined:
    Sep 6, 2013
    Posts:
    19
    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....
     
  12. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    Try putting
    Code (CSharp):
    1. using UnityEngine;
    At the top of the script.
    (Just a guess, I'm away from Unity right now)
    P.
     
  13. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    853
    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.
     
  14. HaddicusGames

    HaddicusGames

    Joined:
    May 28, 2014
    Posts:
    27
    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,

     
  15. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    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
     
  16. KeithKong

    KeithKong

    Joined:
    May 31, 2015
    Posts:
    73
    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!
     
  17. HaddicusGames

    HaddicusGames

    Joined:
    May 28, 2014
    Posts:
    27
    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.

    antennaWorking.gif
     
    Intense_Gamer94 likes this.
  18. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    Nice one man! That looks really fun :)
     
  19. xesf

    xesf

    Joined:
    Nov 24, 2014
    Posts:
    1
    Thanks petey, working like a charm ;)
     
  20. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,208
    Thanks @xesf! Glad it's working for you. :)