Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PhysicsMaterial control.

Discussion in 'Physics for ECS' started by AlexandrBuryakov, Aug 7, 2019.

  1. AlexandrBuryakov

    AlexandrBuryakov

    Joined:
    May 30, 2018
    Posts:
    31
    Hello.
    I direct two objects to each other. (I establish PhysicsVelocity).
    After collision they stick together. How to make a rebound?
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    In the Physics Shape inspector, there's a field called "Restitution". If set to 0, there will be no bounce, and if set to 1, the bounce will be perfectly elastic.
     
  3. AlexandrBuryakov

    AlexandrBuryakov

    Joined:
    May 30, 2018
    Posts:
    31
    I use in such a way. https://docs.unity3d.com/Packages/com.unity.physics@0.1/manual/getting_started.html
    Code (CSharp):
    1.  
    2. using System;
    3. using Unity.Physics;
    4. using Unity.Entities;
    5. using Unity.Mathematics;
    6. using Unity.Collections;
    7. using UnityEngine;
    8. using Unity.Transforms;
    9. using Collider = Unity.Physics.Collider;
    10.  
    11. public class SpawnRandomPhysicsBodies : MonoBehaviour
    12. {
    13.    public GameObject prefab;
    14.    public float3 range;
    15.    public int count;
    16.  
    17.    void OnEnable() { }
    18.  
    19.    public static void RandomPointsOnCircle(float3 center, float3 range, ref NativeArray<float3> positions, ref NativeArray<quaternion> rotations)
    20.    {
    21.        var count = positions.Length;
    22.        // initialize the seed of the random number generator
    23.        Unity.Mathematics.Random random = new Unity.Mathematics.Random();
    24.        random.InitState(10);
    25.        for (int i = 0; i < count; i++)
    26.        {
    27.            positions[i] = center + random.NextFloat3(-range, range);
    28.            rotations[i] = random.NextQuaternionRotation();
    29.        }
    30.    }
    31.  
    32.    void Start()
    33.    {
    34.        if (!enabled) return;
    35.  
    36.        // Create entity prefab from the game object hierarchy once
    37.        Entity sourceEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);
    38.        var entityManager = World.Active.EntityManager;
    39.  
    40.        var positions = new NativeArray<float3>(count, Allocator.Temp);
    41.        var rotations = new NativeArray<quaternion>(count, Allocator.Temp);
    42.        RandomPointsOnCircle(transform.position, range, ref positions, ref rotations);
    43.  
    44.        BlobAssetReference<Collider> sourceCollider = entityManager.GetComponentData<PhysicsCollider>(sourceEntity).Value;
    45.        for (int i = 0; i < count; i++)
    46.        {
    47.            var instance = entityManager.Instantiate(sourceEntity);
    48.            entityManager.SetComponentData(instance, new Translation { Value = positions[i] });
    49.            entityManager.SetComponentData(instance, new Rotation { Value = rotations[i] });
    50.            entityManager.SetComponentData(instance, new PhysicsCollider { Value = sourceCollider });
    51.        }
    52.  
    53.        positions.Dispose();
    54.        rotations.Dispose();
    55.    }
    56. }
    57.  
    At such option there is no PhysicsShape.
    In Inspector components ceased to be displayed. (Only my components are displayed). When you choose them in EntityDebuger. However in EntityDebuger the fact of their existence is displayed.