0

I've been attempting to make a falling object kill a player, but i still want to make sure the player can touch and interact with the object to move it around without dying. The code i've made so far somewhat works as it wont kill the player when they move it will kill the player when it falls and hits them, but the problem is it only works if they have not moved yet from their spawn position (could also be true for when they have stood still for a second). Here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoxyKill : MonoBehaviour
{
    public Rigidbody2D rgbd;
    public CharacterController2D character;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            Debug.Log("Hit player");
            if (rgbd.velocity.y <= -5f) {
                character.Damage(10);
                Debug.Log("Hit player 2");
            }
        }
    }
}

Note - the 'rgbd' is the boxs and this script is attatched to the object

Thanks!

UPDATE

So as of DMGregorys suggestions i have updated the code and while it works i have encountered the same problem (however a lot more controlable this time)

Here is my current code

    public Rigidbody2D _body;
    private float minimumDamageThreshold=10;
    private float collisionDamageScale=.0001f;


    void OnCollisionEnter2D(Collision2D collision)
    {

        Vector2 normal = collision.GetContact(0).normal;
        Vector2 impulse = ComputeTotalImpulse(collision);

        Vector2 myIncidentVelocity = _body.velocity - (impulse / _body.mass);

        Vector2 otherIncidentVelocity = Vector2.zero;
        var otherBody = collision.rigidbody;
        if (otherBody != null)
        {
            otherIncidentVelocity = otherBody.velocity;
            if (!otherBody.isKinematic)
                otherIncidentVelocity += impulse / otherBody.mass;
        }

        // Compute how fast each one was moving along the collision normal,
        // Or zero if we were moving against the normal.
        float myApproach = Mathf.Max(0f, Vector3.Dot(myIncidentVelocity, normal));
        float otherApproach = Mathf.Max(0f, Vector3.Dot(otherIncidentVelocity, normal));

        float damage = otherApproach - myApproach;
        Debug.Log(damage);
        character.Damage(damage);
    }
    static Vector2 ComputeTotalImpulse(Collision2D collision)
    {
        Vector2 impulse = Vector2.zero;

        int contactCount = collision.contactCount;
        for (int i = 0; i < contactCount; i++)
        {
            var contact = collision.GetContact(0);
            impulse += contact.normal * contact.normalImpulse;
            impulse.x += contact.tangentImpulse * contact.normal.y;
            impulse.y -= contact.tangentImpulse * contact.normal.x;
        }

        return impulse;
    }
}

It now works and will kill the player, and i have it printing out the damage in the debug for the character script damage function. The only problem is, it will constantly damage the player if it touches the box at all, and the player needs to be able to move the box around so this will slowly kill it. This could be a problem with the variable amounts that i have configured due to a lack of understanding (and a lot of mindless fideling to be honest).

ConnnK
  • 23
  • 8

0 Answers0