Napivo
Game Addict
I am working on a physics-based game as a hobby in my free time, I am far from a professional programmer.
I don't expect anyone to make any improvements to this silly procedure that is not working as expected.
What I would like to learn is how to find what is wrong and how you would find the error.
I think the way to go would be to create a new program in c# outside unity, that will allow any input(range) and display a graph in that case.
Is that the way?
Any input would be appreciated.
I don't expect anyone to make any improvements to this silly procedure that is not working as expected.
What I would like to learn is how to find what is wrong and how you would find the error.
I think the way to go would be to create a new program in c# outside unity, that will allow any input(range) and display a graph in that case.
Is that the way?
Any input would be appreciated.
C#:
private void OnCollisionEnter2D(Collision2D collision)
{
// Get the Rigidbodies from that are coliding
Rigidbody2D other = collision.gameObject.GetComponent<Rigidbody2D>();
Rigidbody2D myself = gameObject.GetComponent<Rigidbody2D>();
// Get the direction of Movement od the Bodies
Vector2 MyMovement = myself.velocity;
Vector2 OtherMovement = other.velocity;
// Get the movement between bodies
Vector2 InpactMovement = MyMovement - OtherMovement;
// Get mass of the bodies
float MyMass = myself.mass;
float OtherMass = other.mass;
// Get how soft a body is
float SoftnessMe = myself.sharedMaterial.bounciness;
float SoftnessOther = other.sharedMaterial.bounciness;
// Set things if we hit floor
if (other.name == "Floor")
{
SoftnessOther = Napivo.Constants.GroundSoftness;
OtherMass = Napivo.Constants.GroundMass;
}
// Get the inpact delta 1-Soft = hardness
float InpactD = ((1 - SoftnessOther) * (1 - SoftnessMe));
// Get the impact force
// Force = Mass * Velocity
float InpactForceMe = (InpactMovement * OtherMass).magnitude * InpactD;
// From now on we decide the dilimiters of the health lost
float DamageMe = InpactForceMe < HealthThresholdMin ? InpactForceMe : 0;
float Quota = 1;
if ((DamageMe > HealthThresholdMin) && (DamageMe < HealthThresholdMax))
{
Quota = Mathf.Max(DamageMe, 0) / (HealthThresholdMax - HealthThresholdMax);
DamageMe *= Quota;
}
if (!((other.name == "Floor") || (myself.name == "Floor")))
{
if (DamageMe > HealthThresholdMin)
{
Debug.Log("** Me " + myself.name + " Inpacted with " + other.name + " **");
Debug.Log("SoftnessOther = " + SoftnessOther + " SoftnessMe = " + SoftnessMe);
Debug.Log("InpactD = " + InpactD);
Debug.Log("Inpact = " + Mathf.Round(InpactForceMe));
}
}
if (InpactForceMe > HealthThresholdMin)
{
Health -= InpactForceMe;
}
}