Beispiel #1
0
    private void Move(bool horizontal, float axisInput)
    {
        var vector   = horizontal ? _speedXManager.CalculateSpeed(axisInput) : _speedYManager.CalculateSpeed(axisInput);
        var rayPoint = SelectRayPoint(!horizontal ? axisInput : 0, horizontal ? axisInput : 0);

        if (rayPoint != null)
        {
            float raySize = 0.1f;

            var hit = Physics2D.Raycast(rayPoint.transform.position, vector, raySize);

            Vector3 rayVector = new Vector3
            {
                x = !horizontal? 0: vector.x > 0 ? raySize : raySize * -1,
                y = horizontal ? 0 : vector.x > 0 ? raySize : raySize * -1
            };

            Debug.DrawRay(rayPoint.transform.position, rayVector, Color.red);

            if (hit.collider != null)
            {
                if (!hit.collider.name.ToLower().Contains("background"))
                {
                    transform.Translate(vector);
                }
            }
            else
            {
                transform.Translate(vector);
            }
        }
    }