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

Unity Physics Getting Started help

Discussion in 'Entity Component System' started by Tegleg, Mar 9, 2020.

  1. Tegleg

    Tegleg

    Joined:
    Jun 10, 2013
    Posts:
    79
    hello

    im currently looking to convert a physics heavy game to the new unity physics and ecs but stumbled at the first hurdle.
    on the getting started page here https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/getting_started.html
    it gives a code example but im getting an error with 'ForEach'
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Transforms;
    3. using Unity.Physics;
    4. using Unity.Mathematics;
    5. using UnityEngine;
    6.  
    7. public class AttractSystem : ComponentSystem
    8. {
    9.     public float3 center;
    10.     public float maxDistanceSqrd;
    11.     public float strength;
    12.  
    13.     protected override unsafe void OnUpdate()
    14.     {
    15.         ForEach(
    16.             ( ref PhysicsVelocity velocity,
    17.               ref Translation position,
    18.               ref Rotation rotation) =>
    19.             {
    20.                 float3 diff = center - position.Value;
    21.                 float distSqrd = math.lengthsq(diff);
    22.                 if (distSqrd < maxDistanceSqrd)
    23.                 {
    24.                     // Alter linear velocity
    25.                     velocity.Linear += strength * (diff / math.sqrt(distSqrd));
    26.                 }
    27.             });
    28.     }
    29. };
    i created a new c# script and pasted this code. the article isnt very specific on what to do or where to put this script even if it compiles.

    how do i fix the error
    where do i put the script
    how do i define 'center' in the scene
    where do i put the 'AttractComponent' script (next on the page)
    ???

    please could someone help i just cant get my head round any of this

    thanks