Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MKFusion · Apr 28 at 09:18 PM · startdotsinitialization

"OnStart" for each PhysicsVelocity entity ?


I have a bunch of entities that I need to give an initial velocity

physicsVelocity.Linear = velocity


How would I do that for all entities, as OnStartRunning in system is only called once and as soon as there is one entity in scene it stops ? So basically I would need something like Start() in MonoBehaviour, but for each entity.


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gaggedegg · Apr 29 at 06:18 AM 0
Share

You can use "foreach" functionality provided by entity manager to loop through all entities with "physicsVelocity" component. Assign your initial velocity there, OR you can give an initial velocity when you are assigning a "physicsVelocity" component to an entity. Take a look at this ECS documentation from unity.

avatar image MKFusion gaggedegg · Apr 30 at 09:21 PM 0
Share

Found a similar way, thx.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andrew-lukasik · Apr 30 at 10:40 AM


What you need to do is to add:

  • VelocityDelta & PhysicsVelocity components to an entity

OR

  • VelocityDeltaComponent & PhysicsBody components to a GameObject (that you convert into an entity)


VelocityDeltaSystem looks for entities with VelocityDelta components then:

  1. applies an one-time change in it's PhysicsVelocity.Linear

  2. removes the VelocityDelta component (so the vel change won't reapeat)

    note: VelocityDeltaSystem executes only when the matching entities exist and is idle otherwise.


VelocityDeltaComponent.cs

 using UnityEngine;
 using Unity.Entities;
 using Unity.Jobs;
 using Unity.Mathematics;
 using Unity.Transforms;
 using Unity.Physics;
 
 [DisallowMultipleComponent]
 public class VelocityDeltaComponent : MonoBehaviour, IConvertGameObjectToEntity
 {
     [SerializeField] public Vector3 _delta = new Vector3{ z=10 };
     public void Convert ( Entity e , EntityManager cmd , GameObjectConversionSystem cs )
         => cmd.AddComponentData( e , new VelocityDelta{ Value = _delta } );
     #if UNITY_EDITOR
     void OnDrawGizmosSelected ()
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawLine( transform.position , transform.position + transform.TransformDirection(_delta) );
     }
     #endif
 }
 
 public struct VelocityDelta : IComponentData
 {
     public float3 Value;
 }
 
 public partial class VelocityDeltaSystem : SystemBase
 {
     EndFixedStepSimulationEntityCommandBufferSystem _commandBufferSystem;
     protected override void OnCreate ()
     {
         _commandBufferSystem = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
     }
     protected override void OnUpdate ()
     {
         var cmd = _commandBufferSystem.CreateCommandBuffer().AsParallelWriter();
         
         Entities
             .ForEach( ( ref PhysicsVelocity velocity , in VelocityDelta delta , in LocalToWorld transform , in Entity e , in int nativeThreadIndex ) =>
             {
                 velocity.Linear += math.mul( delta.Value , math.inverse( (float3x3) transform.Value ) );
                 cmd.RemoveComponent<VelocityDelta>( nativeThreadIndex , e );
             } )
             .WithBurst()
             .ScheduleParallel();
         
         _commandBufferSystem.AddJobHandleForProducer( this.Dependency );
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MKFusion · Apr 30 at 09:22 PM 0
Share

Indeed, it works. I found a different approach as well, but this one might be more useful. Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

C# property keeps getting overwritten 2 Answers

Race conditions using Instantiate 1 Answer

Can I use WWW in Start()? 1 Answer

Canvas:SendWillRenderCanvases () running after Start() causing issues 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges