Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Circular Gravity Force

Discussion in 'Assets and Asset Store' started by LaneMax, Dec 13, 2013.

  1. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey lyndontroy sorry for the late response, I think this should work sounds like a cool idea. You should just be able to setup a CGF with a type of 'Gravitational Attraction' and sent the force to positive, you will need to use the fix above for the alignment, and yes it does consider mass when using the 'Gravitational Attraction'. I dont have a manual, but I do have the video tutorials under 'Tools->CGF3->Tutorial Videos', I'm also working on a web wiki here later in the future. Hope this helps let me know if you get it working.

    Cheers!
     
    andrewjason likes this.
  2. pauldrummond

    pauldrummond

    Joined:
    Oct 30, 2014
    Posts:
    75
    That would be great because I wouldn't have to worry about updates breaking any custom code.
     
  3. pauldrummond

    pauldrummond

    Joined:
    Oct 30, 2014
    Posts:
    75
    Hi Lane,

    If I use gravitational attraction on a capsule CGF will this force be towards/away from the capsule axis, or along the axis? I seem to be getting a mixture of both. My objects 'fall' out from the axis towards the surrounding structure, but also seem to get pushed along the axis. I understand this is probably how it's meant to work. Unfortunately I'm trying to get the effect of objects being pushed outwards from the axis of a spinning cylinder.

    I could fake the effect for a single object by placing a CGF on the axis of the cylinder and make it track along to match position, then rotate to point straight 'down' through the y-axis of the object. Applying a force along this axis would simulate spin-g. However, I need to simulate a full environment so a global force would be easier.

    Thanks.
     
    Last edited: Jul 3, 2015
  4. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hmm having a hard time picturing it, you have a screenshot? Also is there a reason you are using 'gravitational attraction' for a capsule shape?
     
  5. pauldrummond

    pauldrummond

    Joined:
    Oct 30, 2014
    Posts:
    75
    I've probably got the forces stuff wrong, but my reasoning was that I'm simulating spin-g, where 'down' is outwards from the axis of rotation that runs through the cylinder. It isn't from the centre of a sphere.

     
  6. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey pauldrummond,

    Yeah I don't think that will work with a capsule shape, because the pivot point is going to be at the end of the capsule and not in the center. You might be able to get away with just using a sphere, but the alignment might look a little weird.
     
  7. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey pauldrummond,

    Here is a preview on the next update ;)

    Screen Shot 2015-07-19 at 5.12.49 PM.png
     
    wetcircuit likes this.
  8. andrewjason

    andrewjason

    Joined:
    Oct 17, 2013
    Posts:
    24
    Hi, I'm struggling to get any character controllers working on the planets, everything else seems to work as shown in your tutorial videos, but as soon as a character controller is added to any rigid body object it seems that the CGF just stops having any effect on it. Any tips to make it work?
     
    tralewski likes this.
  9. soundwav_

    soundwav_

    Joined:
    Apr 23, 2013
    Posts:
    12
    Hi :) I have a few questions about CGF, regarding the planet example.
    • Does the planet's gravity accelerate the body that's being pulled to it? (i.e. The gravitational force is stronger the closer you are to the planet).
    • Is it possible to have the planet's red-zones overlap each other?
    Thanks!
     
  10. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    One of the reasons it isn't effected by the CGF is because it doesn't use a rigidbody. To get you on the right track you might be able to use the RigidbodyFPSController rather than the FPSController, but you will have to change the code so that it works accordingly to the planet and not just the y-axis. The RigidbodyFPSController prefab is located under ‘CircularGravityForce Package->Assets->Unity Standard Assets->Characters->FirstPersonCharacter->Prefabs->RigidBodyFPSController’. For the most part you'll have to make your own custom character controllers that uses a rigidbody. Let me know if you have anymore questions, hope this helps.

    Cheers!
     
    Last edited: Jul 27, 2015
    theANMATOR2b and andrewjason like this.
  11. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    If you have the Force Type set to 'Gravitational Attraction' you should get the effect you want. Also the red-zones are just CGFs prefab, and yes you can overlap them and get some cool effects =)
     
  12. Pendrokar

    Pendrokar

    Joined:
    Mar 26, 2011
    Posts:
    95
    What do you suggest in dealing with high speed projectiles? Even setting the collision detection to "Continuous" for rigidbodies of projectiles still makes the force field have no effect, but do ricochet against basic colliders.

    Sure as I lower Unity's "Fixed Timestep" force fields start having an effect on those projectiles. But I am not sure how smart it is to lower that setting, performance wise. Also the force field's power has to be set very high, which might not actually be a problem as such a force field can be set as meant only for high speed projectiles(tagged).

    Here is what I want to achieve:
    1) Deflection of projectiles with a hemisphere force field.
    2) Catching such projectiles between two opposite direction force fields.

    1. For a hemisphere force field, I used a full sphere force field with a collision box as a trigger area to slice its effect. I guess I could simply use a basic collider without the force field for this. That is until I decide whenever the force is equally distributed for the hemisphere force field.

    2. I want to have two force fields so that the projectile would act as if it is caught in a web and swinging forward and backward until air drag stops it. The way Neo in the Matrix stops bullets can be easily mimicked by having an increased drag* within a region that also disables gravity. But then the force field would have no effect and would lack the web effect. Having both the pushing and pulling force field overlap doesn't seem like a good idea, the other alternative I have found is to have a trigger collision sphere where the pulling force no longer pulls objects within it. All well and good, but the high speed of those projectiles is the real problem.

    * I guess you could add drag as another special effect feature.
     
  13. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    So how I would approach this is by making a positive CGF circular gameobject, and a circular trigger that when OnTriggerEnter would set that gameobjects Rigidbody.velocity = 0, allowing the CGF to push it out of the way, if that makes sense. I would make sure that the trigger is smaller than the CGF, like the diagram linked below.

    Diagram.png

    For the trigger scripted it would be somthing like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StopTrigger : MonoBehaviour
    5. {
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         if (other.rigidbody != null)
    9.         {
    10.             other.rigidbody.velocity = Vector3.zero;
    11.         }
    12.     }
    13. }
    Let me know if this helps, cheers!
     
  14. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Have some good updates coming soon, stay tuned! :D
     
    TonanBora and hopeful like this.
  15. Pendrokar

    Pendrokar

    Joined:
    Mar 26, 2011
    Posts:
    95
    @#113

    Yeah before you said it, I did it just like that. Still feeling uneasy about it. Starting to think games that I would be interested in making become simulations.

    Here is the result. For now Circular Gravity is only used for an extra push after almost stopping bullets.


    Next up, the slingshot.
     
    Last edited: Aug 16, 2015
    LaneMax likes this.
  16. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Wow that's pretty sweet, keep me posted on updates!
     
  17. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
  18. Pendrokar

    Pendrokar

    Joined:
    Mar 26, 2011
    Posts:
    95
    By the way again on the high speed projectile issue...

    You did not mention that there is a public method for Circular Gravity called "Apply Force". But then again I tried to execute it within "OnCollisionEnter" while removing collision, but the physics engine still performed its collision. Seems "Continuous" calculation for collisions has no effect for "OnTriggerEnter". The other way I see is to perform a raycast for each bullet on every update and then re-position the bullet to the force field and apply the force. That should work for a constant force, but I still would like to try gravitational attraction(positive), which would be a harder calculation to perform. Could ask an one astrophycist that I know of on how to do such calculation between frames.
     
  19. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey Pendrokar,

    That's kind of my bad, the apply force method shouldn't be exposed and should be private, you might get some weird behavior if you try using it, its currently private in 3.10 the newest version.
     
  20. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Version 3.10 is currently live, here is the release notes:

    3.10
    -- Re-Masked Inspector GUI for CircularGravity.cs and CircularGravity2D.cs, looks alot cleaner and makes more sense
    -- Added all new icons for CircularGravity, Controls, Mods, and Triggers
    -- Added items to Hierarchy menu 'Create -> 3D Object -> CGF' and '2D Ojbect -> CGF 2D' for quickly creating CGF's
    -- Added items to toolbar under 'Tools -> CGF -> Quick CGF' and 'Tools -> CGF -> Quick CGF 2D' with shortcuts for quickly creating CGF's
    -- Added 'Align Direction' under 'Constraints Properties' for CircularGravity.cs and CircularGravity2D.cs
    -- Added context menu items for CircularGravity.cs and CircularGravity2D.cs, makes it a lot easier to add controls, mods, or trigger scripts
    -- Added prototype 'Homeing Rocket' test scene
    -- Updated CGF Wizard, and Toolbar menu items

    Cheers!
     
    hopeful likes this.
  21. Mr.Stein

    Mr.Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @LaneMax, with your asset can do something like this?



    I mean work in a flat/floor base or need a circular base like the planet 2d?
     
  22. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    raycast
    Yeah you should be able to rig up some characters pretty easily using my stuff with some raycast CGF shapes. The easiest way would be doing it from the floor like in the video, but you could also do it from a planet if you wanted, it would be a bit more challenging though ;)
     
  23. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    492
    Ohhhh, more goodies to play around with?
    :-D
     
  24. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    Hello there,
    I need to know how to use for instance the standard assets third person controller (like ethan) to walk on a planet set up with your package.
    thank you
     
  25. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hmmm, give me a little time tonight and I'll try playing with it =) never really tried playing with the third person controller in the scene.
     
  26. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    Much appreciated LaneMax
     
  27. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Ok so after some messing around, here is what I've learned. Doing this is totally be possible! The challenging part for you is going to be getting the ThirdPersonController.cs script working with a planet, because in its current state it only working with a plane and not a sphere. Here is a screenshot of my current setup to point you in the right durection and you'll notice the controls somewhat work. Again the challenge is going to be changing the ThirdPersonController.cs script. Hope this helps, please keep me updated, I'd love to see it in action. Also PS, make sure you use version 3.20f1, there is some changes that help with using Unity's Standard Assets.

    ScreenShot.png
     
  28. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    42
    Using the latest version of CGF and unity 5.3.1f. If i make an object with add the Circular Gravity 2D component and change the force type to Gravitational attraction. it works but throws lots of errors. that kills my performance. Here are my errors

    Rigidbody2D.AddForce(force) assign attempt for 'Astorid1 (2)' is not valid. Input force is { NaN, NaN }.
    UnityEngine.Rigidbody2D:AddForce(Vector2, ForceMode2D)
    CircularGravityForce.CircularGravity2D:ApplyForce(Rigidbody2D, Transform) (at Assets/CircularGravityForce Package/Scrips/CircularGravity/CircularGravity2D.cs:599)
    CircularGravityForce.CircularGravity2D:CalculateAndEstimateForce() (at Assets/CircularGravityForce Package/Scrips/CircularGravity/CircularGravity2D.cs:680)
    CircularGravityForce.CircularGravity2D:FixedUpdate() (at Assets/CircularGravityForce Package/Scrips/CircularGravity/CircularGravity2D.cs:574)

    This is making a blank screen, adding a gameobject, adding the circular gravity 2D script and changing force type to gravitational attraction.

    Am i doing something wrong? everything else in your package seems like it works but gravitational attraction
     
  29. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Thanks for the message, I'll try and take a look tonight and I'll get back with you.
     
  30. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    42
    I've been playing more and will like to clarify somethings. I'm trying to make an asteroids type game where each asteroid has it's own CFG. If I make an asteroid with a rigidbody 2D, circle collier 2D and have a 2D CFG centered inside the collier. It throws tons of errors as if it doesn't know what way to add force to the gameobject. If i set the CFG to an offset of the game object it will work fine since it can pull it in some direction.

    I also tried to use the gameobject filter and set the "Don't Effect Listed GameObjects" and added it's self. But this doesn't seem to be working.

    2015-12-22.png
     
  31. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    So after looking it over, looks like you found a pretty good bug. Looks like when using CircularGravity2D.cs with a rigidbody2D, Collider2D, and using 'Gravitational Attraction' on the same gameobject you get some weird behavour.

    For a work around try this (Screenshot Attached):
    1) Create a 'CGF 2D' gameobject by 'Tools->CGF->Quick CGF 2D'
    2) Put the 'CGF 2D' gameobject under the Rock gameobject as a child
    3) Go to the 'CGF 2D' gameobject and drag and drop the Rock gameobject under the 'Filter Options->Gameobject Filter->Gameobject List' and make sure that 'Game Object Filter Options' is 'Dont Effect Listed Gameobjects'.

    I'll try and get this QA'ed and fixed in the next update, I'll keep you updated when its live. Thanks again for letting me know.

    Cheers!

    ScreenShot.png
     
    hopeful likes this.
  32. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Also here is a code fix if you cant wait for the patch

    Replace the Case Statement starting at line 599 in the CircularGravity2D.cs:
    Code (CSharp):
    1. case ForceType2D.GravitationalAttraction:
    2.     rigid.AddForce((rigid.gameObject.transform.position - trans.position).normalized * rigid.mass * ForcePower / (rigid.gameObject.transform.position - trans.position).sqrMagnitude, _forceMode2D);
    3.     break;
    With this Case Statement:
    Code (CSharp):
    1. case ForceType2D.GravitationalAttraction:
    2.     Vector3 appForce = (rigid.gameObject.transform.position - trans.position).normalized * rigid.mass * ForcePower / (rigid.gameObject.transform.position - trans.position).sqrMagnitude;
    3.     if (float.IsNaN(appForce.x) && float.IsNaN(appForce.y) && float.IsNaN(appForce.z))
    4.     {
    5.         appForce = Vector3.zero;
    6.     }
    7.     rigid.AddForce(appForce, _forceMode2D);
    8.     break;
     
  33. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    42
    it works thanks!
     
    LaneMax likes this.
  34. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Sweet!
     
  35. TymNetwork

    TymNetwork

    Joined:
    Jan 16, 2014
    Posts:
    71
    Is there ramp for the FPS player to impulse himself to a target? What scripts should I add to the FPS to launch him?
    Using similar and much older packages, a ridgidbody character won't project in newer versions.
     
  36. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey Tym,

    If you import the newest Unity's "Standard Assets" here https://www.assetstore.unity3d.com/en/#!/content/32351 there should be two different types of FPS prefabs. FPSController.prefab, and RigidBodyFPSController.prefab, use the RigidBodyFPSController.prefab. You could ether make your own ramp using my CGF stuff or you could copy and paste one of the ramp from the Planets.scene to use. Make sure you use the RigidBodyFPSController, because the standard Character prefab controller wont work just because how the character control takes over the physics. Let me know if you have any problems.

    Cheers!
     
  37. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey Devastadus,

    3.21f1 is currently live containing the fix for the NaN error =) you should be good to go
     
  38. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Just started the next big cgf version, Unity 5.3 is really opening up some good possibilities... stay tuned ;)

    screenshot.png
     
    mdotstrange and hopeful like this.
  39. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    If I wanted my ship to orbit a planet (i'm testing 2D) does this have a method for that just using the gravity? Basically I'd like the ship to orbit the planet on it's own using the gravity.
     
  40. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Hey wintermuteai1,

    Yeah you should be able to get this effect. You will need to give the ship a initial velocity to get it to orbit, and set the Force Type to 'Gravitational Attraction', and the Force Mode to 'Acceleration'.
     
    Last edited: Feb 13, 2016
  41. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Big new version out!



    4.00f1
    -- Added 'Box' type under 'Shapes' for 3D and 2D. NOTE: Requires Unity 5.3 or higher.
    -- Reduced memory usage by 90%!!!
    -- Added Non-Alloc physics support under 'Memory Properties' for 3D and 2D. NOTE: Requires Unity 5.3 or higher for 3D.
    -- Updated all gizmos for all physics 'Shapes' and 'Force Types' for 3D and 2D.
    -- Added new editor handles for all physics 'Shapes' for sizing 3D and 2D cgf objects.
    -- Added 'See Colliders' and 'See Raycast Hits' under 'Memory Properties' for 3D and 2D.
    -- Added memory usage bar under 'Memory Properties' for 3D and 2D when using 'Non-Alloc Physics'
    -- Updated wizard form for the new changes.
    -- Updated inspector GUI for 3D and 2D.
    -- Updated all 'Controls', 'Mods' and 'Trigger' classes for the new changes.
    -- Renamed CircularGravity.cs to CGF.cs
    -- Renamed CircularGravity2D.cs to CGF2D.cs
    -- Renamed CircularGravity_Editor.cs to CGF_Editor.cs
    -- Renamed CircularGravity2D_Editor.cs to CGF2D_Editor.cs
    -- Renamed CircularGravity_Tool.cs to CGF2D_Tool.cs
     
    TonanBora, mdotstrange and hopeful like this.
  42. mdotstrange

    mdotstrange

    Joined:
    Sep 23, 2013
    Posts:
    211
    Wow! Great update! Thank you :)
     
  43. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Thx!
     
  44. RichyK

    RichyK

    Joined:
    Feb 8, 2015
    Posts:
    44
    Is Circular Gravity Force suitable for use on Android mobile devices? Is it highly optimised?

    Thanks,

    Richard
     
  45. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Should work really well especially with the new non-alloc physics support under the 'Memory Properties'
     
  46. Infrid

    Infrid

    Joined:
    Jan 14, 2012
    Posts:
    67
    Hi, what's the unity 5 status for this? Is it compatible? Have you tried it?
    And iOS What's performance like?
     
  47. JohnRossitter

    JohnRossitter

    Joined:
    Dec 18, 2013
    Posts:
    1,027
    I used it in Unity 5 today.
    Works great!
    I was testing it with my asset Zone Controller:
     
    julianr and LaneMax like this.
  48. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Works even bested in Unity 5! And should work well on mobile with the new non-alloc physics support under the 'Memory Properties'. In my older trailer I do show some iOS action:

     
    Last edited: Feb 20, 2016
  49. LaneMax

    LaneMax

    Joined:
    Aug 12, 2013
    Posts:
    181
    Cool!
     
  50. Infrid

    Infrid

    Joined:
    Jan 14, 2012
    Posts:
    67
    Ok,

    great to know.. almost sold me a license.. 2 more questions.
    1. Does it play nice with 2d/sprites?
    2. What's mobile performance like?
    Thanks