public void Execute() { int index = 0; int maxIndex = DeferredImpulseReader.ForEachCount; DeferredImpulseReader.BeginForEachIndex(index++); while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex) { DeferredImpulseReader.BeginForEachIndex(index++); } while (DeferredImpulseReader.RemainingItemCount > 0) { var impulse = DeferredImpulseReader.Read <DefferedCharacterControllerImpulse>(); while (DeferredImpulseReader.RemainingItemCount == 0 && index < maxIndex) { DeferredImpulseReader.BeginForEachIndex(index++); } PhysicsVelocity pv = PhysicsVelocityData[impulse.Entity]; PhysicsMass pm = PhysicsMassData[impulse.Entity]; Translation t = TranslationData[impulse.Entity]; Rotation r = RotationData[impulse.Entity]; if (pm.InverseMass > 0.0f) { pv.ApplyImpulse(pm, t, r, impulse.Impulse, impulse.Point); PhysicsVelocityData[impulse.Entity] = pv; } } }
public void Execute(ref BurstPointComponent burstPoint, ref LocalToWorld position, ref FireDurationComponent fireDuration, ref PlayerInputComponent playerInput) { if (!playerInput.IsFiring) { return; } if (fireDuration.FireDuration <= burstPoint.ShootRate) { fireDuration.FireDuration += DeltaTime; } else { fireDuration = new FireDurationComponent { }; var entity = CommandBuffer.Instantiate(burstPoint.Bullet); Translation localToWorld = new Translation { Value = new float3(position.Value.c3.x, position.Value.c3.y, position.Value.c3.z) }; //SetComponent works faster than AddComponent CommandBuffer.SetComponent(entity, localToWorld); PhysicsVelocity velocity = new PhysicsVelocity { Linear = position.Forward * burstPoint.Speed, Angular = float3.zero }; CommandBuffer.SetComponent(entity, velocity); } }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var chunkVelocities = chunk.GetNativeArray(Velocity); var chunkMasses = chunk.GetNativeArray(Mass); var chunkDirections = chunk.GetNativeArray(Direction); var chunkSpeeds = chunk.GetNativeArray(Speed); for (var i = 0; i < chunk.Count; i++) { var mass = chunkMasses[i]; mass.InverseInertia = new float3(0); chunkMasses[i] = mass; // if (!chunkIsOnGrounds[i].IsGrounded) continue; var velocity = chunkVelocities[i]; var direction = chunkDirections[i].Value; if (new float3(0, 0, 0).Equals(direction)) { continue; } var speed = chunkSpeeds[i].Value; ; direction = speed * math.select(math.normalizesafe(direction), direction, math.length(direction) > 1); direction.y = velocity.Linear.y; chunkVelocities[i] = new PhysicsVelocity { Angular = velocity.Angular, Linear = direction }; } }
public void Execute( Entity entity, int index, [ReadOnly] ref Bone.InitializeData init, //ref Translation pos, //ref Rotation rot, ref PhysicsVelocity v ) { var basepos = this.Translations[init.PostureEntity]; var baserot = this.Rotations[init.PostureEntity]; var pos = this.Translations[entity]; // var rot = this.Rotations[entity]; // pos.Value = basepos.Value + math.mul(baserot.Value, pos.Value); rot.Value = math.mul(baserot.Value, rot.Value); v = new PhysicsVelocity { Linear = float3.zero, Angular = float3.zero }; this.Translations[entity] = pos; // this.Rotations[entity] = rot; // Commands.RemoveComponent <Bone.InitializeData>(index, entity); }
protected override void OnGamePresentationUpdate() { PhysicsVelocity velocity = SimWorld.GetComponent <PhysicsVelocity>(SimEntity); bool spriteLookingRight = _spriteLookingRight; if (SimWorld.HasComponent <DoodleStartDirection>(SimEntity)) { spriteLookingRight = SimWorld.GetComponent <DoodleStartDirection>(SimEntity).IsLookingRight; } int lookDir = 0; if (velocity.Linear.x > (fix)0.05f) { lookDir = 1; } else if (velocity.Linear.x < -(fix)0.05f) { lookDir = -1; } if (lookDir != 0) { Quaternion rot = _bone.localRotation; Vector3 eulerAngles = rot.eulerAngles; eulerAngles.y = (lookDir == 1 ^ spriteLookingRight) ? 180f : 0f; rot.eulerAngles = eulerAngles; _bone.localRotation = rot; } }
protected override void OnStartRunning() { Debug.Log("Started Running"); myWorld = World; positions = new List <float3>(); velocities = new List <float3>(); positions2 = new List <List <float3> >(); velocities2 = new List <List <float3> >(); isSimulating = false; shouldRollBack = false; velocityToAdd = float3.zero; Entities.ForEach((ref PhysicsVelocity physicsVelocity, ref Translation position, ref Rotation rotation) => { if (rotation.Value.value.w == 1f) { ballVel = physicsVelocity; ballPos = position; } else { batVel = physicsVelocity; batPos = position; } }); }
public void Move(ref PhysicsVelocity rb, ref PhysicsMass mass, ref InputStruct input, ref PhysicsControllerStruct playerData, ref LocalToWorld toWorld, ref Rotation rot, ref Translation trans) { float3 targetVelocity = new float3(); float speed = 9; float gravity = 4; //Set target to input velocity targetVelocity.x = input.move.x; targetVelocity.z = input.move.y; //Calculate how fast we should be moving targetVelocity = TransformDirection(toWorld.Value, targetVelocity); //Change from local space to world space targetVelocity *= speed * deltaTime * 100; //Apply a force that attempts to reach our target velocity float3 velocityChange = (targetVelocity - rb.Linear); velocityChange.y = -gravity * deltaTime * 10; //Apply gravity to the player //Mouse movement rb.Angular.y = -input.mouseX * 2; //* deltaTime; mass.InverseInertia[0] = 0; mass.InverseInertia[2] = 0; if (playerData.isGrounded && input.jump && playerData.timeSinceLastJump > .1f) { velocityChange.y = 10; playerData.timeSinceLastJump = 0; } playerData.timeSinceLastJump += deltaTime; rb.ApplyLinearImpulse(mass, velocityChange); }
protected override void OnUpdate() { bool goalReached = false; EntityManager manager = World.EntityManager; Entity player = GetSingletonEntity <Player>(); Entities.ForEach((Entity entity, ref Goal tag, ref Ground activation) => { if (activation.playerWasHere) { goalReached = true; } }); if (goalReached) { // Make the player entity stick to the platform ComponentDataFromEntity <PhysicsVelocity> physicsVelocity = GetComponentDataFromEntity <PhysicsVelocity>(); PhysicsVelocity playerData = physicsVelocity[player]; playerData.Linear = new Unity.Mathematics.float3 { x = 0, y = 0, z = 0 }; physicsVelocity[player] = playerData; GameManager.GetInstance().HasReachedGoal(); } }
public unsafe void Execute <TCtx>(TCtx ctx, InputTriggerPort port) where TCtx : IGraphInstance { var entity = ctx.ReadEntity(Entity); if (entity == Unity.Entities.Entity.Null) { return; } if (!ctx.EntityManager.HasComponent <PhysicsMass>(entity) || !ctx.EntityManager.HasComponent <PhysicsVelocity>(entity)) { return; } PhysicsMass physicsMass = ctx.EntityManager.GetComponentData <PhysicsMass>(entity); PhysicsVelocity physicsVelocity = ctx.EntityManager.GetComponentData <PhysicsVelocity>(entity); if (LinearOnly) { physicsVelocity.ApplyLinearImpulse(physicsMass, ctx.ReadFloat3(Value)); } else { Translation t = ctx.EntityManager.GetComponentData <Translation>(entity); Rotation r = ctx.EntityManager.GetComponentData <Rotation>(entity); ComponentExtensions.ApplyImpulse(ref physicsVelocity, physicsMass, t, r, ctx.ReadFloat3(Value), ctx.ReadFloat3(Point)); } ctx.EntityManager.SetComponentData(entity, physicsVelocity); }
protected override void OnUpdate() { var player = GetSingletonEntity <Player>(); ComponentDataFromEntity <PhysicsVelocity> velocities = GetComponentDataFromEntity <PhysicsVelocity>(); PhysicsVelocity velocity = velocities[player]; if (isPaused) { if (!hasSaved) { hasSaved = true; savedVelocity = velocity.Linear; } velocity.Linear = new float3 { x = 0, y = 0, z = 0 }; velocities[player] = velocity; } else { velocity.Linear = savedVelocity; velocities[player] = velocity; isPaused = false; hasSaved = false; } }
protected override void OnUpdate() { Entities.WithAll(ComponentType.ReadOnly <SpawnningTag>(), ComponentType.ReadOnly <LocalToWorld>(), ComponentType.ReadWrite <WaterSpawner>()).ForEach((ref WaterSpawner waterSpawnner, ref LocalToWorld localToWorld) => { waterSpawnner.TimeBetweenSpawns += Time.DeltaTime; if (waterSpawnner.TimeBetweenSpawns >= waterSpawnner.WaterSpawnRate) { Entity water = PostUpdateCommands.Instantiate(waterSpawnner.WaterEntity); Translation translation = new Translation { Value = localToWorld.Position }; PhysicsVelocity velocity = new PhysicsVelocity { Linear = localToWorld.Forward * waterSpawnner.WaterPressure }; PostUpdateCommands.SetComponent(water, translation); PostUpdateCommands.SetComponent(water, velocity); } } ); }
public static void SetVel(ref PhysicsVelocity vel) { vel.Angular = float3.zero; float speed = Vector3.Magnitude(vel.Linear); vel.Linear *= SPEED / speed; }
// Was curious about EntityA and EntityB in triggerEvent.ColliderKeys, // In my example EntityA was always a ball (entity which was moving), maybe if trigger is moving, this will swap them in pair (ball & trigger) public void Execute(TriggerEvent triggerEvent) { if (physicsVelocityEntities.HasComponent(triggerEvent.Entities.EntityA)) { PhysicsVelocity physicsVelocity = physicsVelocityEntities[triggerEvent.Entities.EntityA]; physicsVelocity.Linear.y = 5f; physicsVelocityEntities[triggerEvent.Entities.EntityA] = physicsVelocity; } }
public bool IsUpdated(EntityManager entityManager, Entity entity) { pv = entityManager.GetComponentData <PhysicsVelocity>(entity); if (!pv.Angular.Equals(Angular) || !pv.Linear.Equals(Linear)) { return(false); } return(true); }
// Get the linear velocity of a rigid body at a given point (in world space) public static float3 GetLinearVelocity(this PhysicsVelocity velocityData, PhysicsMass massData, Translation posData, Rotation rotData, float3 point) { var worldFromEntity = new RigidTransform(rotData.Value, posData.Value); var worldFromMotion = math.mul(worldFromEntity, massData.Transform); float3 angularVelocity = math.rotate(worldFromMotion, velocityData.Angular); float3 linearVelocity = math.cross(angularVelocity, (point - worldFromMotion.pos)); return(velocityData.Linear + linearVelocity); }
protected override void OnUpdate() { var commandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter(); var input = GetSingleton <CharacterGunInput>(); float dt = Time.DeltaTime; Entities .WithName("CharacterControllerGunToManyInputJob") .WithBurst() .ForEach((Entity entity, int entityInQueryIndex, ref Rotation gunRotation, ref CharacterGun gun, in LocalToWorld gunTransform) => { // Handle input { float a = -input.Looking.y; gunRotation.Value = math.mul(gunRotation.Value, quaternion.Euler(math.radians(a), 0, 0)); gun.IsFiring = input.Firing > 0f ? 1 : 0; } if (gun.IsFiring == 0) { gun.Duration = 0; gun.WasFiring = 0; return; } gun.Duration += dt; if ((gun.Duration > gun.Rate) || (gun.WasFiring == 0)) { if (gun.Bullet != null) { var e = commandBuffer.Instantiate(entityInQueryIndex, gun.Bullet); Translation position = new Translation { Value = gunTransform.Position + gunTransform.Forward }; Rotation rotation = new Rotation { Value = gunRotation.Value }; PhysicsVelocity velocity = new PhysicsVelocity { Linear = gunTransform.Forward * gun.Strength, Angular = float3.zero }; commandBuffer.SetComponent(entityInQueryIndex, e, position); commandBuffer.SetComponent(entityInQueryIndex, e, rotation); commandBuffer.SetComponent(entityInQueryIndex, e, velocity); } gun.Duration = 0; } gun.WasFiring = 1; }).ScheduleParallel(); m_EntityCommandBufferSystem.AddJobHandleForProducer(Dependency); }
protected override void OnUpdate() { Entities.ForEach((Entity interactionEntity, ref ProjectileCarInteractionComponent interaction) => { PostUpdateCommands.DestroyEntity(interactionEntity); if (EntityManager.GetComponentData <ActiveComponent>(interaction.Projectile).IsActive) { var carPlayerId = EntityManager.GetComponentData <SynchronizedCarComponent>(interaction.Car).PlayerId; if (carPlayerId != EntityManager.GetComponentData <OwnerComponent>(interaction.Projectile).OwnerPlayerId) { EntityManager.SetComponentData(interaction.Projectile, new ActiveComponent { IsActive = false }); interactionServerSystemGroup.PostUpdateCommands.DestroyEntity(interaction.Projectile); if (!EntityManager.GetComponentData <SynchronizedCarComponent>(interaction.Car).IsShieldActive) { var healthComponent = EntityManager.GetComponentData <HealthComponent>(interaction.Car); if (healthComponent.Health > 0) { healthComponent.Health -= SerializedFields.singleton.projectileDamage; if (healthComponent.Health <= 0) { healthComponent.Health = 0; Utils.KillPlayer(interaction.Car, EntityManager, Entities, PostUpdateCommands); } EntityManager.SetComponentData(interaction.Car, healthComponent); } PhysicsVelocity carVelocity = EntityManager.GetComponentData <PhysicsVelocity>(interaction.Car); Translation carPosition = EntityManager.GetComponentData <Translation>(interaction.Car); Rotation carRotation = EntityManager.GetComponentData <Rotation>(interaction.Car); float3 carTransformUp = math.mul(carRotation.Value, new float3(0, 1, 0)); float3 impactPoint = EntityManager.GetComponentData <Translation>(interaction.Projectile).Value - EntityManager.GetComponentData <PhysicsVelocity>(interaction.Projectile).Linear * 1 / 60f; carVelocity.ApplyImpulse( EntityManager.GetComponentData <PhysicsMass>(interaction.Car), carPosition, carRotation, math.normalize(Vector3.ProjectOnPlane(EntityManager.GetComponentData <PhysicsVelocity>(interaction.Projectile).Linear, carTransformUp)) * SerializedFields.singleton.projectileImpulse, impactPoint - (float3)(Vector3.Project(impactPoint - carPosition.Value, carTransformUp)) ); EntityManager.SetComponentData(interaction.Car, carVelocity); } } } }); }
protected override void OnUpdate() { var input = m_CharacterGunInputQuery.GetSingleton <CharacterGunInput>(); float dt = Time.fixedDeltaTime; Entities.ForEach( (ref LocalToWorld gunTransform, ref Rotation gunRotation, ref CharacterGun gun) => { // Handle input { float a = -input.Looking.y; gunRotation.Value = math.mul(gunRotation.Value, quaternion.Euler(math.radians(a), 0, 0)); gun.IsFiring = input.Firing > 0f ? 1 : 0; } if (gun.IsFiring == 0) { gun.Duration = 0; gun.WasFiring = 0; return; } gun.Duration += dt; if ((gun.Duration > gun.Rate) || (gun.WasFiring == 0)) { if (gun.Bullet != null) { var e = PostUpdateCommands.Instantiate(gun.Bullet); Translation position = new Translation() { Value = gunTransform.Position + gunTransform.Forward }; Rotation rotation = new Rotation() { Value = gunRotation.Value }; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = gunTransform.Forward * gun.Strength, Angular = float3.zero }; PostUpdateCommands.SetComponent(e, position); PostUpdateCommands.SetComponent(e, rotation); PostUpdateCommands.SetComponent(e, velocity); } gun.Duration = 0; } gun.WasFiring = 1; } ); }
void SpawnNewLaser(int index, Vector3 position, float forcedScale, int laserIndex) { Entity newCubeEntity = manager.Instantiate(laserIndex == 0 ? laserEntityPrafab : ballLaserEntityPrefab); Vector3 currentInput = Vector3.forward; // Was trying to alter the forward velocity of the lasers based on a vector3 of current vhz inputs. Didn't work Vector3 direction = currentInput; Vector3 speed = direction * laserSpeed; Translation spawnPoint = new Translation(); Rotation spawnRot = new Rotation(); if (index % 2 == 0) { Translation pos = new Translation() { Value = playerPrefab.transform.position + position }; spawnPoint = pos; Rotation rot = new Rotation() { Value = transform.rotation }; spawnRot = rot; } else { Translation pos = new Translation() { Value = playerPrefab.transform.position }; spawnPoint = pos; Rotation rot = new Rotation() { Value = playerPrefab.transform.rotation }; spawnRot = rot; } PhysicsVelocity velocity = new PhysicsVelocity() { Linear = speed, Angular = Vector3.zero }; Scale scale = new Scale() { Value = forcedScale }; manager.AddComponentData(newCubeEntity, scale); manager.AddComponentData(newCubeEntity, spawnPoint); manager.AddComponentData(newCubeEntity, spawnRot); manager.AddComponentData(newCubeEntity, velocity); }
void SpawnBall() { Entity ball = manager.Instantiate(ballEntityPrefab); Vector3 dir = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? -1 : 1, UnityEngine.Random.Range(-0.5f, 0.5f), 0f).normalized; Vector3 speed = dir * ballSpeed; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; manager.AddComponentData(ball, velocity); }
protected override void OnUpdate() { HandleHookContacts(); var ecb = _ecbSytem.CreateCommandBuffer(); Entities .WithAll <PhysicsVelocity>() .ForEach((Entity entity, ref HookData hookData, in FixTranslation position) => { bool destroy = false; // If hook is uninitialized, initialize it if (hookData.State == HookData.EState.Uninitialized) { hookData.StartPosition = position; hookData.State = HookData.EState.TravelingForward; } // If hook is traveling back (after contact), set its velocity and the contact entity if (hookData.State == HookData.EState.TravelingBack) { fix2 d = hookData.StartPosition - position; fix dLength = length(d); if (dLength < (fix)0.25) { destroy = true; } else { fix2 dir = d / dLength; var newVelocity = new PhysicsVelocity(hookData.TravelBackSpeed * dir); SetComponent(entity, newVelocity); // give same velocity to captured entity if (HasComponent <PhysicsVelocity>(hookData.TouchedEntity)) { SetComponent(hookData.TouchedEntity, newVelocity); } } } if (destroy) { ecb.DestroyEntity(entity); } }).Run(); }
private void SpawnBall() { var ball = _manager.Instantiate(_ballEntityPrefab); var dir = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? 1 : -1, UnityEngine.Random.Range(-0.5f, 0.5f), 0f).normalized; var speed = dir * ballSpeed; var velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; _manager.AddComponentData(ball, velocity); }
void SpawnBall() // 重新生成球体对象 { Entity ball = manager.Instantiate(ballEntityPrefab); // 实例化entity对象 Vector3 dir = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? -1 : 1, UnityEngine.Random.Range(-.5f, .5f), 0f).normalized; Vector3 speed = dir * ballSpeed; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = speed, // 线速度位移 Angular = float3.zero // 角速度 旋转 }; manager.AddComponentData(ball, velocity); // 为实体绑定组件信息 }
void SpawnNewCube() { Entity newCubeEntity = manager.Instantiate(cubeEntityPrefab); Vector3 direction = Vector3.up; Vector3 speed = direction * cubeSpeed; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; manager.AddComponentData(newCubeEntity, velocity); }
public override void UpdateSystem() { ComponentDataFromEntity <Translation> translations = GetComponentDataFromEntity <Translation>(); ComponentDataFromEntity <PhysicsVelocity> velocities = GetComponentDataFromEntity <PhysicsVelocity>(); ComponentDataFromEntity <UnitMove> unitMoves = GetComponentDataFromEntity <UnitMove>(); Entities .WithReadOnly(translations) .ForEach((Entity entity, in Flocker flocker, in DynamicBuffer <FlockingGroup> flockingGroup) => { //Get flocking target Entity target = flocker.flockingTarget; Translation targetTranslation = translations[target]; float3 targetPos = targetTranslation.Value; PhysicsVelocity targetVelocity = velocities[target]; float3 targetDir = math.normalize(targetVelocity.Linear); float targetSpeed = unitMoves[target].moveSpeed; // Get flocking group averages float3 sumDir = default; float3 sumPos = default; for (int i = 0; i < flockingGroup.Length; ++i) { Entity groupedFlocker = flockingGroup[i].entity; Translation groupedTranslation = translations[groupedFlocker]; sumPos += groupedTranslation.Value; PhysicsVelocity groupedVelocity = velocities[groupedFlocker]; sumDir += math.normalize(groupedVelocity.Linear); } float3 averageGroupPos = sumPos / flockingGroup.Length; float3 averageGroupDir = sumDir / flockingGroup.Length; // Combine target values with averages using given ratio float3 averagePos = (targetPos * flocker.targetRatio) + (averageGroupPos * (1 - flocker.targetRatio)); float3 averageDir = (targetDir * flocker.targetRatio) + (averageGroupDir * (1 - flocker.targetRatio)); // Cohesion //Separation //Alignment }).ScheduleParallel();
public void SpawnBall() { ballCounter++; var ball = manager.Instantiate(BallPrefabEntity.prefab); var dir = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? -1 : 1, UnityEngine.Random.Range(-.5f, .5f), 0f).normalized; var speed = dir * 4; var velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; manager.AddComponentData(ball, velocity); }
protected override void OnUpdate() { float dt = Time.fixedDeltaTime; Entities.ForEach( (ref LocalToWorld gunTransform, ref Rotation gunRotation, ref PhysicsGun gun) => { if (gun.isFiring == 0) { gun.duration = 0; gun.wasFiring = 0; return; } gun.duration += dt; if ((gun.duration > gun.rate) || (gun.wasFiring == 0)) { if (gun.bullet != null) { var e = PostUpdateCommands.Instantiate(gun.bullet); Translation position = new Translation() { Value = gunTransform.Position + gunTransform.Forward }; Rotation rotation = new Rotation() { Value = gunRotation.Value }; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = gunTransform.Forward * gun.strength, Angular = float3.zero }; PostUpdateCommands.SetComponent(e, position); PostUpdateCommands.SetComponent(e, rotation); PostUpdateCommands.SetComponent(e, velocity); } gun.duration = 0; } gun.wasFiring = 1; } ); }
public void Execute(ref CharacterAnimator characterAnimator, ref Translation translation) { PhysicsVelocity physicsVelocity = PhysicsVelocityFromEntity[characterAnimator.CharacterEntity]; Character character = CharacterFromEntity[characterAnimator.CharacterEntity]; float speedRatio = math.clamp(math.length(physicsVelocity.Linear) / character.MoveSpeed, 0, 1); characterAnimator.BobMoveValue = new float3(0, math.abs(math.sin(time * characterAnimator.BobFrequency)) * characterAnimator.BobAmount * speedRatio, 0); float3 previousPos = new float3(translation.Value.x, 0, translation.Value.z); translation.Value = math.lerp(previousPos, previousPos + characterAnimator.BobMoveValue, speedRatio); //float vBobValue = ((math.sin(time * anim.BobFrequency * 2f) * 0.5f) + 0.5f) * anim.BobAmount; //float rotBobValue = math.sin(time * anim.BobFrequency) * anim.BobAmount; //rotBobValue *= math.length(velocity.Linear); //rot.Value = math.mul(rot.Value, quaternion.RotateZ(rotBobValue)); }
void SpawnBall() { var ball = entityManager.Instantiate(ballEntityPrefab); var dir = new Vector3( Random.Range(0, 2) == 0 ? -1f : 1f, Random.Range(-0.5f, 0.5f), 0f).normalized; var speed = dir * ballSpeed; var velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; entityManager.AddComponentData(ball, velocity); }
void SpawnBall() { StopCoroutine(CountdownAndSpawnBall()); Entity ball = _manager.Instantiate(_ballEntity); Vector3 direction = new Vector3(UnityEngine.Random.Range(0, 2) == 0 ? -1 : 1, UnityEngine.Random.Range(-.5f, .5f), 0f).normalized; Vector3 speed = direction * ballSpeed; PhysicsVelocity velocity = new PhysicsVelocity() { Linear = speed, Angular = float3.zero }; _manager.AddComponentData(ball, velocity); }