Игрок не выходит на платформу. Кажется, ничего не работает - PullRequest
0 голосов
/ 10 апреля 2020

В настоящее время я работаю над трехмерной мобильной игрой, в которой игрок проводит пальцем, чтобы переместить персонажа. По какой-то причине я не могу заставить персонажа быть отобранным на платформе. Как только персонаж прыгает на вертикально движущейся платформе, он становится шатким, а когда он спрыгивает с платформы, он скользит каждый раз, когда игрок проводит пальцем.

Вот сценарий смахивания:

public class Swipe : MonoBehaviour
{
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDragging = false; 
private Vector2 startTouch, swipeDelta;

private void Update()
{
    tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

    #region Standalone Inputs 
    if (Input.GetMouseButtonDown(0))
    {
        tap = true;
        isDragging = true; 
        startTouch = Input.mousePosition; 
    }

    else if(Input.GetMouseButtonUp(0))
    {
        isDragging = false; 
        Reset(); 
    }
    #endregion

    #region Mobile Inputs
    if(Input.touches.Length > 0)
    {
        if(Input.touches[0].phase == TouchPhase.Began)
        {
            tap = true;
            isDragging = true; 
            startTouch = Input.touches[0].position;
        }

        else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            isDragging = false; 
            Reset(); 
        }
    }
    #endregion

    // Calculate the distance 
    swipeDelta = Vector2.zero;

    if (isDragging)
    {
        if (Input.touches.Length > 0)
            swipeDelta = Input.touches[0].position - startTouch;

        else if (Input.GetMouseButton(0))
            swipeDelta = (Vector2)Input.mousePosition - startTouch; 
    }

    // Did we cross the deadzone?
    if(swipeDelta.magnitude > 125)
    {
        //swipe direction 
        float x = swipeDelta.x;
        float y = swipeDelta.y;

        if(Mathf.Abs(x) > Mathf.Abs(y))
        {
            // Left or right 
            if (x < 0)
                swipeLeft = true;
            else
                swipeRight = true; 
        }

        else
        {
            // Up or down 
            if (y < 0)
                swipeDown = true;
            else
                swipeUp = true;
        }

        Reset();
    }
}

public void Reset()
{
    startTouch = swipeDelta = (Vector2.zero);
    isDragging = false;
    Movement.anim.SetBool("isWalking", false);
}

public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
}

и это сценарий перемещения персонажа:

public class Movement : MonoBehaviour
{
public Swipe swipeControls;
public Transform player;
private Vector3 desiredPosition;
public static Animator anim;
public TMP_Text distanceText;
public TMP_Text coinText; 
private int distance;

private void Start()
{
    anim = GetComponentInChildren<Animator>();
    distance = 0; 
}

private void FixedUpdate()
{
    if (swipeControls.SwipeLeft)
    {
        desiredPosition += Vector3.left;
        anim.SetBool("isWalking", true);
    }

    if (swipeControls.SwipeRight)
    {
        desiredPosition += Vector3.right;
        anim.SetBool("isWalking", true);
    }

    if (swipeControls.SwipeUp)
    {
        desiredPosition += Vector3.forward;            
        anim.SetBool("isWalking", true);
        distance++; 
    }
    if (swipeControls.SwipeDown)
    {
        desiredPosition += Vector3.back;
        anim.SetBool("isWalking", true);
        distance -= 1; 
    }

    player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 5f * Time.deltaTime);

    distanceText.text = distance.ToString();
    coinText.text = Coin.coin.ToString();
}

private void OnTriggerEnter(Collider other)          
{
    if (other.gameObject.tag == "Platform")
    {
        transform.parent = other.gameObject.transform;
        Debug.Log("Parented");
    }

    if (other.gameObject.tag == "trigger_3")
    {
        desiredPosition += Vector3.down;
        player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 5f * Time.deltaTime);
    }

}

private void OnTriggerExit(Collider other)
{
    transform.parent = null;
    Debug.Log("Detatched");
    desiredPosition += Vector3.zero;
    Debug.Log("Zero Pos");
    player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 5f * Time.deltaTime);
    Debug.Log("Player transform 2");
}
}

Это сводит меня с ума. Пожалуйста, помогите!

...