2
votes

I'm fairly new to unity and have encountered a issue to where when one of my game objects collide with the other then they both take damage. I have the damage taking down, but the issue comes when i ram into the opponent it takes damage but i as well take damage. Trying to wrap my head around how to fix so that he only takes damage from my impact and if the other game object runs into me then i take damage only and not him.

public void TakeDamage(float amount)
{
    health -= amount;
    healthbar.fillAmount = health / starthealth;


    if (health <= 0)
    {
        gameObject.SetActive(false);
    }

}

public void OnCollisionEnter(Collision collision)
{
    if (collision.collider.gameObject.tag == "player") 
    {
        collision.gameObject.GetComponent<playerScript> ().TakeDamage (damage);

    }
}

This is what the take damage and collision looks like, the enemy collides with me and it uses the take damage method from the player script

2
Well, if my car drives into yours, we both take damage, so there is a lot of logic in that happening. You could decide that maybe the faster moving item was the one that is causing the damage perhapsBugFinder
This is about c#, not unityscript.Ruzihm

2 Answers

1
votes

I think you would need to differentiate between Hitboxes and Hurtboxes. (A good Article about these here)

Then when a Hitbox collides with a Hurtbox only the Hurtbox's Character takes damage.

The method of how to handle this depends on what kind of game you're working on. A melee brawler type of game would probably use different Collider Components to differentiate between Hitbox and Hurtbox.

Another more complicated scenario would be a "car crashing" type of game where cars take damage when crashing into each other. Since the Hitbox and the Hurtbox on a car are basically the same you would need additional logic to determine what car takes what amount of damage.

I could think of the amount of damage the other car takes to be dependent on the car`s speed at the time of the crash and the actual direction the car is facing.

So when Player 1 attacks/crashes into Player 2 only Player 2 would receive damage since Player 2 either didn't have any speed or didn't drove into the direction of Player 1.

With a game-logic like this, you can most often cover all different edge cases of damage taking and assign the right amount of damage to each player.

0
votes

If both of your objects also have rigidbody components, then you can check both their previous velocities, and assign damage to the one with the lower velocity. I understand this is what you mean conceptually with your ramming description. To ensure that the velocity before the collision is used (and not the one already resolved after the collision), you can store the collision in the FixedUpdate function. So you get (presuming a Player class attached to both objects, to use your term):

using UnityEngine;

public class Player : MonoBehaviour
{
    Vector3 velocityBeforeCollisions = Vector3.zero;
    Rigidbody body = null;
    float health = 1f;

    void Start()
    {
        body = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        velocityBeforeCollisions = body.velocity;
    }

    public float GetSpeed()
    {
        return velocityBeforeCollisions.magnitude;
    }

    void OnCollisionEnter(Collision collision)
    {
        Player otherPlayer = collision.gameObject.GetComponent<Player>();
        if (otherPlayer != null && otherPlayer.GetSpeed() > GetSpeed())
        {
            Damage();
        }
    }

    public void Damage()
    {
        Debug.Log(name + " damaged");
        health -= 0.1f;
    }

}