Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Torus in Unity

Discussion in 'Scripting' started by ratamorph, Jan 24, 2008.

  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Have any of you guys done or is there a script out there that will generate a torus (not just the collition but the geometry aswell). I'm trying to draw the orbits of the planets in the solar system and I think a torus might be exactly what I need, the problem with importing it from Maya is that I need tori of diferent radius but when I scale, the thickness of the torus also increases.
     
  2. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    here is it.
     

    Attached Files:

  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Though, a line renderer would probably be better for orbits...
     
  4. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    thanks for the script steffen, StarManta, I haven't tried the line renderer much, but would it be easy to make it draw circles or ellipses?
     
  5. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Hey Steffen, how does this script work? is it an editor script? how do I use it?
     
  6. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    attache the script to a gameobject.
    set the segmentRadius, tubeRadius, segments and tubes in the editor.

    If you push the play button the turos woud be created.

    if you need a wizard i can write you one.
     
  7. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Thanks Steffen, I got it working, but when I get close to the torus or at certain angles it becomes invisible, any clue why? I looked at the code and didn't see anything that could produce this behavior.
     
  8. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    have you checked the near clip plane from the camera ?
     
  9. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I was thinking that's the problem, but why would the torus disapear completely, usually when things get clipped by the camera planes just part of the mesh gets clipped, not the whole thing, that's what has me soo confused...
     
  10. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The script doesn't assign any normals as far as I can see. If I remember correctly, Unity assigns a (0, 0, 1) normal by default to each vertex (same as Vector3.forward), which will make the shading go wrong. All faces will be shaded exactly the same and the lightness will depend on the angle between the light source and the object.

    Since you are doing a planetary motion thing, would I be right in thinking your background colour is black? Your torus would appear black (and therefore invisible against the background) if it were rotated to face exactly away from the light source.

    Unless you want the shaded, tube-like effect of the torus then it probably would be best to use a LineRenderer for this task. Wikipedia has a page with the equations for drawing an ellipse - the parametric version is probably the easiest way to generate the verts for the LineRenderer and also to plot the path to animate the motion of the planets.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You could try putting in a Mesh.RecalculateNormals(); line in there (probably near RecalculateBounds();).

    --Eric
     
  12. KlaRo115

    KlaRo115

    Joined:
    Feb 24, 2006
    Posts:
    675
    Assign "mesh renderer" to the torus, I think, it's going work.
     
  13. joaeba

    joaeba

    Joined:
    Apr 8, 2014
    Posts:
    1
    Inserting mesh.RecalculateNormals(); just below RecalculateBounds(); worked for me. Thx for the nice script.
     
  14. thieberson

    thieberson

    Joined:
    Mar 6, 2015
    Posts:
    5
    Hi, Guys. This is an old post, but I think it is still important.
    The script to create the ring is very useful. Congratulations.

    I've made some changes to possiblity to manipulate the ring's parameters outside the script.

    I hope it helps.
     

    Attached Files:

    Last edited: May 4, 2015
    StealthHunter likes this.
  15. zoller27osu

    zoller27osu

    Joined:
    Dec 6, 2015
    Posts:
    1
    Thanks for the scripts, Steffen and thieberson!

    I started simplifying them earlier though and they ended up with 30+ fewer lines of code. xD

    I'm pretty sure they'll run faster, but they're definitely at least smaller and less memory intensive.

    Both simplified scripts are in the zip.

    I did verify myself that the end result is the exact same for the default parameters, but let me know if there are any problems in either of them!
     

    Attached Files:

    StealthHunter likes this.
  16. holyfingers

    holyfingers

    Joined:
    Feb 2, 2012
    Posts:
    4
    Hey there, I'd just like to add my thanks to everybody here for making my life so much easier today! :)

    In the off chance it'll be useful to anybody else searching around, I've made a Playmaker action based on what's already been done here: http://hutonggames.com/playmakerforum/index.php?topic=11786.0

    Cheers,

    Nick
     
    Seraphic572 likes this.
  17. eMsylf

    eMsylf

    Joined:
    Mar 9, 2018
    Posts:
    10
    Hey everyone, I really needed a torus shape in my current project, and I stumbled upon this script. After trying it out I was still not satisfied. I was looking for something that would allow me to create a torus like any other basic shape such as a cube, sphere, or capsule.

    After some tinkering using the scripts provided in the zip posted by zoller27osu, I have made a fully functional editor script for this exact purpose.

    Optimization
    It might not be as optimized as it used to be. I welcome anyone to take a look at my edits and improve on it.

    How to use
    To create a torus, go to the menu at top of the Unity window:
    1. GameObject
    2. 3D Object
    3. Torus

    Other info
    This script collection has a README file attached that explains how to use it.
     

    Attached Files:

    CringeItIn likes this.
  18. CringeItIn

    CringeItIn

    Joined:
    Jan 24, 2021
    Posts:
    9
    Thank you very much Sir!