#When I spawn units through the scripts their position and rotation turns into NaN and Inf

1 messages · Page 1 of 1 (latest)

lofty bramble
#

I am suspecting its the apply Impulse code that is adding velocity to the rigid body. Not sure though and I am having difficulty debugging it.

 [BurstCompile]
    public partial struct ApplyAgentImpulseJob : IJobEntity {
        public float DeltaTime;

        [BurstCompile]
        public void Execute(Entity _entity,
            RigidBodyAspect rigidBodyAspect,
            in ApplyImpulse _applyImpulseOnKeyData,
            in AgentConfiguration ac) {
            if (math.length(_applyImpulseOnKeyData.Direction) >
                0.0f) {
                if (!math.isnan(_applyImpulseOnKeyData.Direction.x) &&
                    !math.isnan(_applyImpulseOnKeyData.Direction.y) &&
                    !math.isnan(_applyImpulseOnKeyData.Direction.z)) {
                    Debug.Log(_applyImpulseOnKeyData.Direction);
                    rigidBodyAspect.ApplyLinearImpulseWorldSpace(_applyImpulseOnKeyData.Direction * DeltaTime);
                }
            }
            if (math.length(rigidBodyAspect.LinearVelocity) > ac.Speed) {
                float3 temp = rigidBodyAspect.LinearVelocity;
                temp = math.normalize(temp) * ac.Speed;
                rigidBodyAspect.LinearVelocity = temp;
            }
        }
    }

Any suggestions on how to debug. Maybe i am miss understanding how physics works, or to work with the update Groups (when to trigger it).

Thank you for any pointer or ideas!!!

gilded cosmos
#

use normalizeSafe

lofty bramble
#

I tried that and its not working. The ApplyImpulse component is already written with normalizeSafe)

Could it have something todo with were i execute the code:

[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
gilded cosmos
#

no, it' s unrelated

#

basically you assign Nan values somewhere

#

and that leads to whole transform becoming Nan

#

so just need to figure out where

#

try to debug each system which writes those components with conditional of one of values being NaN

#

this is how I caught such problem on my side

#

to me it was related to either normalization or rotation without using safe overload

#

I had direction of 0,0,0 which lead to NaN result

#

safe solves it

lofty bramble
#

hmm I;ll have to keep looking Right now am running the Physicscode just with a fixed value and it still happens


 public void OnUpdate(ref SystemState state) {
    
        state.Dependency = new ApplyAgentImpulseJob {
            DeltaTime = SystemAPI.Time.fixedDeltaTime
        }.Schedule(state.Dependency);
    }


 [BurstCompile]
    public partial struct ApplyAgentImpulseJob : IJobEntity {
        public float DeltaTime;

        [BurstCompile]
        public void Execute(Entity _entity,
            RigidBodyAspect rigidBodyAspect,
            in ApplyImpulse _applyImpulseOnKeyData,
            in AgentConfiguration ac) {
            if (math.length(_applyImpulseOnKeyData.Direction) >
                0.0f) {
               
               rigidBodyAspect.ApplyLinearImpulseWorldSpace(math.normalizesafe(new float3(1,0,0)) * DeltaTime);

                    }
            
           
        }
    }
#

Maybe instead of applying an impulse I should just manage the speed myself

#

and rotate the linear Velocity vector

gilded cosmos
#

no need in burst attribute on Execute btw

#

so regarding NaN - just try to find when value becomes NaN

#

if this job doesn't have it - try other

#

SystemAPI.Time.fixedDeltaTime btw, this is wrong

#

should always use normal Delta time

lofty bramble
#

I am trying the various suggestions. But I have not yet found why this breaks.

Has how we can define prefabs as a blueprint for an entity changed recently? I am trying to spawn all components now manually, just to pindown whats happening.

gilded cosmos
lofty bramble
#

Yea if I have an agent that’s in the scene as a prefab and then baked by the converter it works fine. but if I reference it and then use it I. The instantiation it fails .