コード例 #1
0
    private void Start()
    {
        carInfo = gameObject.GetComponent <SphereCarController>();
        voidParticles.SetActive(false);

        _originalMat = voidWaspParts[0].GetComponent <Renderer>().material;

        // Set reference to Car Heat manager
        carHeatInfo = gameObject.GetComponent <CarHealthBehavior>();
    }
コード例 #2
0
    private void OnCollisionEnter(Collision collision)
    {
        CarCollision other = collision.gameObject.GetComponent <CarCollision>();

        if (other != null && _invulTime <= 0f)
        {
            _invulTime = .5f;
            Rigidbody           rb               = GetComponent <Rigidbody>();
            Rigidbody           other_rb         = other.GetComponent <Rigidbody>();
            SphereCarController otherCar         = other.car.GetComponent <SphereCarController>();
            CarHealthBehavior   otherCarHeat     = other.car.GetComponent <CarHealthBehavior>();
            Vector3             relativeVelocity = rb.velocity - other_rb.velocity;
            Vector3             relativePosition = rb.position - other_rb.position;
            collisionEffectsScript.CreateSparks(collision);
            AudioManager.instance.Play("General collision", transform);

            //calculating the angle in radians then converting it to degrees
            float angleBetween = Mathf.Acos(Vector3.Dot(relativeVelocity.normalized, relativePosition.normalized)) * 180 / Mathf.PI;

            // We only want the player instigating the collision to damage the other so this angle helps determing who the instigating player is
            float instigatingPlayerAngle = Mathf.Acos(Vector3.Dot(rb.velocity.normalized, -relativePosition.normalized)) * 180 / Mathf.PI;

            frontCollisionParticles.GetComponent <ParticleSystem>().Play();

            // If the angleBetween < 90 then the collision exists
            // If instigatingPlayerAngle < 90 then that player is moving in the direction of the other player and should assign damage
            if (angleBetween < 90 & instigatingPlayerAngle < 90)
            {
                float percentDamage = 0f;
                if (rb.velocity.magnitude > 10f)
                {
                    percentDamage = Vector3.Project(relativePosition.normalized, rb.velocity.normalized).magnitude;
                }
                if (percentDamage > 0)
                {
                    float damage = percentDamage * weight * relativeVelocity.magnitude;
                    otherCarHeat.healthCurrent -= damage;
                }
            }
        }
    }