Example #1
0
    public void ReboundRaycast(RaycastHit2D preHit, float residueDistance, Vector2 incomingVector)
    {
        //获得反射向量
        Vector2 reflectVector = Vector2.Reflect(incomingVector, preHit.normal);
        Vector2 startPoint    = preHit.point - incomingVector.normalized * sr.size.y * 0.5f - reflectVector.normalized * sr.size.y * 0.5f + reflectVector.normalized * 0.0001f;

        RaycastHit2D hit = Physics2D.Raycast(startPoint, reflectVector, residueDistance, LayerMask.GetMask(maskLayer));


        float currDistance;

        if (hit.collider)
        {
            currDistance = hit.fraction * residueDistance;
        }
        else
        {
            currDistance = residueDistance;
        }

        Raycast    raycast     = Instantiate((GameObject)Resources.Load("Bullet/Raycast/Raycast")).GetComponent <Raycast>();
        Quaternion newRotation = Quaternion.FromToRotation(Vector2.right, reflectVector);

        raycast.ConfigRaycast(currDistance, fireWeapon, residueTimes - 1, startPoint, newRotation, maskLayer, raycastHoldTime);
        nextRaycast = raycast;
        float nextRaycastDistance = residueDistance - currDistance;

        if (nextRaycast.residueTimes > 0 && nextRaycastDistance > 0)
        {
            nextRaycast.ReboundRaycast(hit, nextRaycastDistance, reflectVector);
        }
    }
Example #2
0
    public void Fire()
    {
        ShakeCameraAndRepel();
        shakeCameraStepTimer = 0.0f;

        Vector2      direction           = CalTargetDirection(firePoint.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.position);
        RaycastHit2D hit                 = Physics2D.Raycast(firePoint.transform.position, direction, raycastDistance, LayerMask.GetMask(maskLayer));
        float        currRaycastDistance = 0;

        if (hit.collider)
        {
            currRaycastDistance = hit.fraction * raycastDistance;
        }
        else
        {
            currRaycastDistance = raycastDistance;
        }
        Raycast raycast = Instantiate((GameObject)Resources.Load("Bullet/Raycast/Raycast")).GetComponent <Raycast>();

        raycast.ConfigRaycast(currRaycastDistance, this, reboundTimes, firePoint.transform.position, weaponPoint.transform.rotation, maskLayer, raycastHoldTime);
        if (raycast.residueTimes > 0 && hit.collider)
        {
            raycast.ReboundRaycast(hit, (1 - hit.fraction) * raycastDistance, direction);
        }

        if (weaponType == EWeaponType.coutinue || weaponType == EWeaponType.storagePowerAndCoutinue)
        {
            raycast.transform.parent = weaponPoint.transform;
        }
        currRaycast = raycast;
    }