protected override void OnUpdate()
    {
        var   commandBuffer = commandBufferSystem.CreateCommandBuffer();
        float deltaTime     = Time.DeltaTime;

        float3 direction = new float3(1, 0, 1);

        Entities.WithStructuralChanges().
        WithAll <ApplyImpulseTag>().
        ForEach((Entity entity, ref PhysicsVelocity velocity, ref PhysicsGravityFactor gravityFactor, in Rotation rot, in PhysicsCollider physicsCollider, in DeadRobotData deadRobotData, in PhysicsMass mass) =>
        {
            Translation bulletTrans = EntityManager.GetComponentData <Translation>(deadRobotData.bullet);
            Translation trans       = EntityManager.GetComponentData <Translation>(entity);
            EntityManager.SetComponentData(entity, PhysicsMass.CreateDynamic(new MassProperties()
            {
                AngularExpansionFactor = mass.AngularExpansionFactor,
                MassDistribution       = new MassDistribution()
                {
                    Transform     = mass.Transform,
                    InertiaTensor = new float3(1, 1, 1)
                },
                Volume = 1
            }, 1));
            PhysicsMass newMass = EntityManager.GetComponentData <PhysicsMass>(entity);
            float3 impulse      = math.normalize(bulletTrans.Value - trans.Value) * 10;
            gravityFactor.Value = 1;
            PhysicsComponentExtensions.ApplyExplosionForce(ref velocity, newMass, physicsCollider, trans, rot, 100, bulletTrans.Value, 5, 1.0f / 30.0f, new float3(0, 1, 0), 3, ForceMode.Impulse);
            EntityManager.RemoveComponent <ApplyImpulseTag>(entity);
        })
    protected override void OnUpdate()
    {
        InputComponent userInput = GetSingleton <InputComponent>();


        /// Get the physics world.
        // You don't need to do this if you're going to use ComponentExtensions.ApplyLinearImpulse, but I have kept the code in to show you how to do it.
        PhysicsWorld physicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>().PhysicsWorld;

        float3 playerPos = float3.zero;

        Entities
        .ForEach((
                     Entity _entity,
                     ref PhysicsVelocity _physicsVelocity,
                     ref PhysicsMass _physicsMass,
                     ref Translation translation, in ControlledByIUserInput controlledByIUserInput) =>
        {
            // if (userInput.MouseDown)
            // {
            //     translation.Value.x = userInput.MousePos.x;
            //     translation.Value.y = userInput.MousePos.y;
            // }

            _physicsVelocity.Linear.z = 0;
            /// Apply a linear impulse to the entity.
            PhysicsComponentExtensions.ApplyLinearImpulse(ref _physicsVelocity, _physicsMass, new float3(userInput.MoveInput * 0.03f, 0));
            playerPos = translation.Value;
        })
        .WithoutBurst()
        .Run();

        if (userInput.ShootInput)
        {
            Entities
            .ForEach((
                         Entity _entity,
                         ref PhysicsVelocity _physicsVelocity,
                         ref PhysicsMass _physicsMass,
                         in Translation translation, in Tag_Ball tag_ball) =>
            {
                _physicsVelocity.Linear.z = 0;
                if (math.distance(new float2(playerPos.x, playerPos.y), new float2(translation.Value.x, translation.Value.y)) < 0.6f)
                {
                    var shootDir = translation.Value - playerPos;
                    /// Apply a linear impulse to the entity.
                    PhysicsComponentExtensions.ApplyLinearImpulse(ref _physicsVelocity, _physicsMass, shootDir * 1.5f);
                }
            })
        protected override void OnUpdate()
        {
            Entities.ForEach((
                                 ref PhysicsVelocity _velocity, ref MovementCommandsComponentData _commandsComponentData,
                                 ref Rotation _rotation, in MovementParametersComponentData _parametersComponentData, in PhysicsMass _physicsMass) =>
            {
                PhysicsComponentExtensions.ApplyAngularImpulse(
                    ref _velocity, _physicsMass,
                    _commandsComponentData.m_currentAngularCommand * _parametersComponentData.m_angularVelocity);

                var currentAngularSpeed = PhysicsComponentExtensions.GetAngularVelocityWorldSpace(in _velocity, in _physicsMass, in _rotation);

                if (math.length(currentAngularSpeed) > _parametersComponentData.m_maxAngularVelocity)
                {
                    PhysicsComponentExtensions.SetAngularVelocityWorldSpace(ref _velocity, _physicsMass, _rotation,
                                                                            math.normalize(currentAngularSpeed) * _parametersComponentData.m_maxAngularVelocity);
                }
            }).Schedule();
Example #4
0
        protected override void OnUpdate()
        {
            float deltaTime = Time.DeltaTime;

            // Input (old system) from UnityEngine since DOTS native input system doesn't exist yet
            float horizontalInput = Input.GetAxis("Horizontal");
            float verticalInput   = Input.GetAxis("Vertical");

            Entities
            .WithBurst()
            .WithAll <Player>()
            .ForEach((ref PhysicsVelocity velocity, ref PhysicsMass physicsMass, in Movement movement) =>
            {
                // Set direction of impulse based on player input
                float3 direction = new float3(horizontalInput, 0.0f, verticalInput);

                // Apply Linear Impulse from Physics Extension methods
                PhysicsComponentExtensions.ApplyLinearImpulse(ref velocity, physicsMass, direction * movement.Force);
            })
            .Schedule();
        }
        protected override void OnUpdate()
        {
            Entities.ForEach((
                                 ref MovementCommandsComponentData _movementCommandsComponentData, ref PhysicsVelocity _velocity,
                                 in MovementParametersComponentData _movementParametersComponentData,
                                 in PhysicsMass _physicsMass, in Translation _translation) =>
            {
                _movementCommandsComponentData.m_previousPosition = _translation.Value;

                PhysicsComponentExtensions.ApplyLinearImpulse(
                    ref _velocity,
                    _physicsMass,
                    _movementCommandsComponentData.m_currentDirectionOfMove *
                    _movementCommandsComponentData.m_currentlinearCommand *
                    _movementParametersComponentData.m_linearVelocity
                    );

                if (math.length(_velocity.Linear) > _movementParametersComponentData.m_maxLinearVelocity)
                {
                    _velocity.Linear = math.normalize(_velocity.Linear) * _movementParametersComponentData.m_maxLinearVelocity;
                }
            }).ScheduleParallel();