Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question When adding physics to an object, it falls through the ground. What's the problem?

Discussion in 'Physics for ECS' started by MrKsi, Sep 10, 2022.

  1. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    When I get into an object, I add Physics Body to it, imitated by Velocity, GravityFactor and Mass components, but for some reason the object falls through the floor, although there is a Physics Shape component on the floor and if I create an object with physics in advance, then everything is fine with it and it does not fall through. How to fix it?
    P.S.I need to add physics to objects only to those that I hit, this is necessary for optimization, because in my game, each object consists of pieces, but if you put physics on them all, then the game will slow down very much, and using this method is not, but there is a problem with a collision, how to fix it?

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Physics;
    3. using Unity.Transforms;
    4. using UnityEngine;
    5. public class Shoot : MonoBehaviour
    6. {
    7.     public GameObject Bullet;
    8.     public Transform PointSpawn;
    9.     public float DistanceShoot = 6.25f;
    10.     public Camera Cam;
    11.  
    12.     private EntityManager _entityManager;
    13.     private BlobAssetStore _blobAssetStore;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         EntityObjectSetup();
    19.     }
    20.  
    21.     private void EntityObjectSetup()
    22.     {
    23.         _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    24.         _blobAssetStore = new BlobAssetStore();
    25.     }
    26.  
    27.  
    28.     private void Update()
    29.     {
    30.         if (Input.GetMouseButton(0))
    31.         {
    32.             var ray = new UnityEngine.Ray(transform.position, transform.forward);
    33.             var entity = ECSUtilits.Raycast(ray.origin, ray.origin + ray.direction * DistanceShoot);
    34.             if(entity != Entity.Null)
    35.             {
    36.                 try
    37.                 {
    38.                     var fragment = _entityManager.GetComponentData<FragmentECS>(entity);
    39.                     fragment.Direction = Cam.transform.forward;
    40.                     fragment.Speed = 1;
    41.                     fragment.IsDelected = true;
    42.                     var physics = _entityManager.GetComponentData<PhysicsMass>(entity);
    43.                     _entityManager.SetComponentData(entity, fragment);
    44.                 }
    45.                 catch
    46.                 {
    47.                     var colliderComponent = _entityManager.GetComponentData<PhysicsCollider>(entity);
    48.                     _entityManager.AddComponentData(entity, PhysicsMass.CreateKinematic(new MassProperties()));
    49.                     var massComponent = _entityManager.GetComponentData<PhysicsMass>(entity);
    50.                     massComponent.InverseMass = 3.0f;
    51.                     massComponent.InverseInertia = 4.0f;
    52.                     _entityManager.SetComponentData(entity, massComponent);
    53.                     _entityManager.AddComponentData(entity, new PhysicsGravityFactor() { Value = 5.0f });
    54.                     _entityManager.AddComponentData(entity, new PhysicsVelocity() { Linear = 0.01f, Angular = 0.05f });
    55.                     Debug.Log("Физика добавлена к объекту: " + entity.Index + " " + _entityManager.GetName(entity));
    56.                 }
    57.             }
    58.         }
    59.     }
    60.  
    61.  
    62.  
    63.     private void OnApplicationQuit()
    64.     {
    65.         _blobAssetStore.Dispose();
    66.     }
    67. }
    68.  
     
  2. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    For some reason, this code works with standard objects
     
  3. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    123
    Ok, I fixed it, I had to add new localToWorld() to the child objects
     
    JosepMariaPujol likes this.