Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by krystian904 · Oct 14, 2021 at 11:26 AM · dots

How to multithread GameObject steering AI with Jobs System

Hi,

I have problem with Job System. I want to send many GameObject to to IJob struct but I cant to send input List and NativeList. I want to execute parts GameObject put to many threads, but I don't know how.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Unity.Jobs;
 using Unity.Collections;
 using Unity.Burst;
 public class carsAI : MonoBehaviour
 {
     public List<GameObject> objects; // -> sends only parts objects to execute 
     void Update ()
     {
        // how to gets parts from the list object and execute in to many threads
     }
 }
 [BurstCompile]
 public struct driveCar : IJob
 {
     private NativeList<GameObject> objectToJobs; 
     public void Execute()
     {
        foreach (GameObject c in objectToJobs) Debug.Log(c.name);
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by andrew-lukasik · Oct 19, 2021 at 09:56 PM

I'm not 100% sure what you want to know, but provided code sample lets me guess some of the problems here. If nothing else remember this:

Burst-compiled (i.e. fast) IJob cannot reference any class type. GameObject is a class so it must be replaced with something else that is a struct (or struct of structs).


Here is a code example of an AI behavior to blindly follow given path. It should answer most of the question marks here: link text link text

CarsAISystem.cs

 // src*: https://gist.github.com/andrew-raphael-lukasik/d65b2d97d88767b938cf226c15216480
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Jobs;
 using Unity.Mathematics;
 using Unity.Jobs;
 using Unity.Collections;
 using Unity.Transforms;
 
 public class CarsAISystem : MonoBehaviour
 {
     [SerializeField] GameObject _carPrefab = null;
     [SerializeField] float _topSpeed = 4;
     [SerializeField] float _acceleration = 2;
     [SerializeField] float _turningRate = 1;
     [SerializeField][Range(0,1)] float _performanceVariation = 0.2f;
     [SerializeField] int _numberOfCarsToSpawn = 10;
 
     Transform[] _cars = null;
     TransformAccessArray _carsTransformAccess;
     NativeArray<int> _carWaypointData;
     NativeArray<float> _carSpeedData;
     NativeArray<float> _carPerformanceData;
 
     [SerializeField] Transform[] _waypoints = new Transform[2];
     NativeArray<float3> _pathVertices;
     
     JobHandle Dependency = default(JobHandle);
     
     void Awake ()
     {
         _cars = new Transform[ _numberOfCarsToSpawn ];
         for( int i=0 ; i<_numberOfCarsToSpawn ; i++ )
         {
             _cars[i] = GameObject.Instantiate(
                 original:    _carPrefab ,
                 position:    (float3)UnityEngine.Random.insideUnitSphere * new float3{ x=30 , y=0 , z=30 } ,
                 rotation:    Quaternion.identity
             ).transform;
         }
         _carsTransformAccess = new TransformAccessArray( _cars );
         _carSpeedData = new NativeArray<float>( _cars.Length , Allocator.Persistent );
         _carPerformanceData = new NativeArray<float>( _cars.Length , Allocator.Persistent );
         for( int i=0 ; i<_carPerformanceData.Length ; i++ )
             _carPerformanceData[i] = UnityEngine.Random.Range( 1f-_performanceVariation , 1f+_performanceVariation );
 
         _pathVertices = new NativeArray<float3>( _waypoints.Length , Allocator.Persistent );
         for( int i=0 ; i<_pathVertices.Length ; i++ )
             _pathVertices[i] = _waypoints[i].position;
 
         _carWaypointData = new NativeArray<int>( _cars.Length , Allocator.Persistent );
         for( int i=0 ; i<_carWaypointData.Length ; i++ )
             _carWaypointData[i] = UnityEngine.Random.Range( 0 , _pathVertices.Length );
     }
 
     void OnDestroy ()
     {
         Dependency.Complete();
         if( _pathVertices.IsCreated ) _pathVertices.Dispose();
         if( _carsTransformAccess.isCreated ) _carsTransformAccess.Dispose();
         if( _carWaypointData.IsCreated ) _carWaypointData.Dispose();
         if( _carSpeedData.IsCreated ) _carSpeedData.Dispose();
         if( _carPerformanceData.IsCreated ) _carPerformanceData.Dispose();
     }
 
     void Update ()
     {
         Dependency.Complete();
         
         var job = new FollowWaypoints{
             DeltaTime        = Time.deltaTime ,
             TopSpeed        = _topSpeed ,
             Acceleration    = _acceleration ,
             TurningRate        = _turningRate ,
             
             Path            = _pathVertices ,
             Waypoint        = _carWaypointData ,
             Speed            = _carSpeedData ,
             Performance        = _carPerformanceData
         };
         Dependency = job.Schedule( _carsTransformAccess , Dependency );
     }
 
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         int length = _waypoints.Length;
         for( int i=0 ; i<length ; i++ )
         {
             var a = _waypoints[i];
             var b = _waypoints[(i+1)%length];
             if( a && b ) Gizmos.DrawLine( a.position , b.position );
         }
     }
     #endif
 
     [Unity.Burst.BurstCompile]
     public struct FollowWaypoints : IJobParallelForTransform
     {
         public float DeltaTime, TopSpeed, Acceleration, TurningRate;
         [NativeDisableParallelForRestriction] public NativeArray<float3> Path;
         public NativeArray<int> Waypoint;
         public NativeArray<float> Speed;
         public NativeArray<float> Performance;
         void IJobParallelForTransform.Execute ( int index , TransformAccess transform )
         {
             float3 position = transform.position;
             float3 forward = math.mul( transform.rotation , new float3{ z=1 } );
             float performance = Performance[index];
             float3 waypointPos = Path[ Waypoint[index] ];
 
             // switch waypoints:
             if( math.lengthsq(waypointPos-position)<math.pow(Speed[index],2f) )
             {
                 // will be there in about a second, lets select next waypoint
                 int next = ( Waypoint[index] +1 )%Path.Length;
                 Waypoint[index] = next;
                 waypointPos = Path[next];
             }
 
             // steering control:
             float3 desiredForward = math.normalize( waypointPos - position );
             float makeSteeringDecreseWithSpeed = math.lerp( 2f , 1f , math.saturate(math.pow(Speed[index]/TopSpeed,2f)) );
             transform.rotation = math.slerp(
                 transform.rotation , quaternion.LookRotation( desiredForward , new float3{y=1} ) ,
                 math.saturate( makeSteeringDecreseWithSpeed * TurningRate*performance * DeltaTime )
             );
 
             // speed control:
             float makeDesiredSpeedDependOnAngleToTarget = math.dot( forward , desiredForward );
             float desiredSpeed = TopSpeed*performance * makeDesiredSpeedDependOnAngleToTarget;
             float desiredSpeedDiff = desiredSpeed - Speed[index];
             Speed[index] += math.min( math.abs(desiredSpeedDiff) , Acceleration*performance ) * math.sign(desiredSpeedDiff) * DeltaTime;
             transform.position += (Vector3)( forward * Speed[index] * DeltaTime );
         }
     }
     
 }

Comment
Add comment · Show 2 · 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 krystian904 · Oct 21, 2021 at 06:58 AM 0
Share

your anwser is amaizing. very thank you.

avatar image andrew-lukasik · Oct 21, 2021 at 10:14 AM 0
Share

( I updated FollowWaypoints to make it a bit easier to understand )

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

168 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 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

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer

Best way to get data from a Monobheaviour in a JobComponentSystem? 2 Answers

[ECS] DefaultWorldInitialization.Initialize take 50MB 1 Answer

Unity (ECS & DOTS) Entity Sinking 0 Answers

How to make singleplayer/multiplayer game with DOTS 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