Hello.
I direct two objects to each other. (I establish PhysicsVelocity).
After collision they stick together. How to make a rebound?
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.
I use in such a way. Simulating | Package Manager UI website
using System;
using Unity.Physics;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Collections;
using UnityEngine;
using Unity.Transforms;
using Collider = Unity.Physics.Collider;
public class SpawnRandomPhysicsBodies : MonoBehaviour
{
public GameObject prefab;
public float3 range;
public int count;
void OnEnable() { }
public static void RandomPointsOnCircle(float3 center, float3 range, ref NativeArray<float3> positions, ref NativeArray<quaternion> rotations)
{
var count = positions.Length;
// initialize the seed of the random number generator
Unity.Mathematics.Random random = new Unity.Mathematics.Random();
random.InitState(10);
for (int i = 0; i < count; i++)
{
positions[i] = center + random.NextFloat3(-range, range);
rotations[i] = random.NextQuaternionRotation();
}
}
void Start()
{
if (!enabled) return;
// Create entity prefab from the game object hierarchy once
Entity sourceEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World.Active);
var entityManager = World.Active.EntityManager;
var positions = new NativeArray<float3>(count, Allocator.Temp);
var rotations = new NativeArray<quaternion>(count, Allocator.Temp);
RandomPointsOnCircle(transform.position, range, ref positions, ref rotations);
BlobAssetReference<Collider> sourceCollider = entityManager.GetComponentData<PhysicsCollider>(sourceEntity).Value;
for (int i = 0; i < count; i++)
{
var instance = entityManager.Instantiate(sourceEntity);
entityManager.SetComponentData(instance, new Translation { Value = positions[i] });
entityManager.SetComponentData(instance, new Rotation { Value = rotations[i] });
entityManager.SetComponentData(instance, new PhysicsCollider { Value = sourceCollider });
}
positions.Dispose();
rotations.Dispose();
}
}
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.