Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        // Get the car velocity in local space
        Vector2 localVel = transform.InverseTransformDirection(rb2d.velocity);

        if (localVel.x > 0f)
        {
            // Do the box casts
            bool front_hit = Physics2D.Raycast(
                rb2d.position,
                Quaternion.Euler(0, 0, rb2d.rotation) * Vector2.right,
                LookAhead * localVel.x, 1 << 8).transform != null;
            bool right_hit = Physics2D.Raycast(
                rb2d.position,
                Quaternion.Euler(0, 0, rb2d.rotation - DeltaAngle) * Vector2.right,
                LookAhead * localVel.x, 1 << 8).transform != null;
            bool left_hit = Physics2D.Raycast(
                rb2d.position,
                Quaternion.Euler(0, 0, rb2d.rotation + DeltaAngle) * Vector2.right,
                LookAhead * localVel.x, 1 << 8).transform != null;

            // If it hits, calculate how the passenger suggests you avoid it
            if (front_hit || right_hit || left_hit)
            {
                // See if the left or the right side or the front was hit
                if (right_hit == left_hit && front_hit)
                {
                    // Frontal hit
                    passenger.BumperCall(Direction.Front, intensity);
                }
                else if (!left_hit && right_hit && front_hit)
                {
                    // Right hit(Even if the front is hit, we should still call out a wall to the right)
                    passenger.BumperCall(Direction.Right, intensity);
                }
                else if (!right_hit && left_hit && front_hit)
                {
                    // Left hit!
                    passenger.BumperCall(Direction.Left, intensity);
                }
            }
        }
    }