projectile bounce

This commit is contained in:
Sveske Juice 2024-04-20 21:55:03 +02:00
parent 2ace1d04ad
commit 73e2459342
5 changed files with 48 additions and 10 deletions

View File

@ -0,0 +1,14 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!134 &13400000
PhysicMaterial:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: WallMat
dynamicFriction: 0
staticFriction: 0
bounciness: 1
frictionCombine: 1
bounceCombine: 1

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: db65845c02126636d9fae11e1ea2f004
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 13400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -146,6 +146,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a98521a4b559621438fc196adc605f3b, type: 3}
m_Name:
m_EditorClassIdentifier:
bounciness: 0.516
bounciness: 1
damage: 10
wallRebounces: 4
comingFrom: {fileID: 0}

View File

@ -335,7 +335,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 643774939}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
@ -440,7 +440,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 769011490}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
@ -745,7 +745,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1085503232}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
@ -850,7 +850,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1197417724}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
@ -955,7 +955,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1621186629}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
@ -1243,7 +1243,7 @@ PrefabInstance:
- target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc,
type: 3}
propertyPath: m_LocalRotation.w
value: -0.13805284
value: -0.1380528
objectReference: {fileID: 0}
- target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc,
type: 3}
@ -1258,7 +1258,7 @@ PrefabInstance:
- target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc,
type: 3}
propertyPath: m_LocalRotation.z
value: -0.13815573
value: -0.13815574
objectReference: {fileID: 0}
- target: {fileID: 8882344377078016156, guid: fd9f9b61c0ebb324ebc9f929e26706bc,
type: 3}
@ -1341,7 +1341,7 @@ BoxCollider:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1797347348}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: db65845c02126636d9fae11e1ea2f004, type: 2}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0

View File

@ -8,6 +8,7 @@ public class Projectile : MonoBehaviour
private float bounciness = 0.5f;
private Collider projCol;
private Rigidbody body;
[SerializeField, Range(0, 50)]
private int damage = 10;
@ -15,20 +16,32 @@ public class Projectile : MonoBehaviour
[SerializeField]
private int wallRebounces = 4;
private int hitWalls = 0;
public HealthComponent comingFrom;
private Vector3 prevVel;
private void Awake()
{
projCol = GetComponent<Collider>();
body = GetComponent<Rigidbody>();
Assert.IsNotNull(projCol);
PhysicMaterial pMat = new();
pMat.bounciness = this.bounciness;
pMat.staticFriction = 0f;
pMat.dynamicFriction = 0f;
pMat.frictionCombine = PhysicMaterialCombine.Minimum;
pMat.bounceCombine = PhysicMaterialCombine.Minimum;
projCol.material = pMat;
}
private void LateUpdate()
{
prevVel = body.velocity;
}
private void OnCollisionEnter(Collision collision)
{
HealthComponent hitHealthComp = collision.gameObject.GetComponent<HealthComponent>();
@ -44,6 +57,8 @@ public class Projectile : MonoBehaviour
{
Destroy(gameObject);
}
Vector3 newVel = Vector3.Reflect(prevVel.normalized, collision.contacts[0].normal.normalized);
body.velocity = newVel.normalized * prevVel.magnitude * bounciness;
}
}
}