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

Planet Gravity example code explanation?

Discussion in 'Entity Component System' started by Tegleg, Feb 8, 2021.

  1. Tegleg

    Tegleg

    Joined:
    Jun 10, 2013
    Posts:
    79
    Hi
    is there an explanation of the code in this example?

    specifically, i am trying to work out how this bit works in PlanetGravityAuthoring.cs
    Code (CSharp):
    1. Entities
    2.             .WithName("ApplyGravityFromPlanet")
    3.             .WithBurst()
    4.             .ForEach((ref PhysicsMass bodyMass, ref PhysicsVelocity bodyVelocity, in Translation pos, [COLOR=#ff0000]in PlanetGravity gravity[/COLOR]) =>
    5.             {...
    where are these bits assigned?
    WithName("ApplyGravityFromPlanet")
    in PlanetGravity gravity

    the end goal really is to understand ECS, in this case i want to effect all spawned 'asteroids' with the gravity of all other asteroids, rather than a single planet.
    how would i do that in ECS starting with this example?

    thank you in advance for any insight into any of this
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    WithName("ApplyGravityFromPlanet") is just for debugging.
    It gives the job a specific name instead of Lambda0 or whatever it defaults to.

    Good for the profiler etc.
     
  3. jasons-novaleaf

    jasons-novaleaf

    Joined:
    Sep 13, 2012
    Posts:
    181
    this is passing the gravity component in by reference, so that it is not copying the struct. the "in" keyword is used instead of "ref" so that it is readonly.