mercredi 1 février 2017

if function in unity not working c#

In my unity c# game for android I would like to see if the closest gameobject to the character is the one im looking for(left) and if the player of the game has not swiped left on the screen(swipeLeft) then I would like to change a value in my code called strike, but It is not working properly in game and Im not sure why.When Im in the game view the leftSwipe value does not work every time (It does when the if function is removed) and strike does not change at all.I would like to know how to fix this or is there is an alternative to this problem

Here is the if statement

if ((closestPlatform = left) && (leftSwipe = false)) { strike = 1; }

Here is the entire c# script



using UnityEngine;
using System.Collections;

public class SwipeChecker : MonoBehaviour
{

    public float maxTime;
    public float minSwipeDistance;

    float startTime;
    float endTime;

    Vector3 startPos;
    Vector3 endPos;

    float swipeDistance;
    float swipeTime;
    public float swipeScore;

    public GameObject left;
    public GameObject right;
    public GameObject up;
    public GameObject down;
    public GameObject swipeChecker;
    public GameObject[] platforms = new GameObject[5];

    public bool leftSwipe;
    public bool didntSwipe;

    public float strike;



    public GameObject closestPlatform;





    // Use this for initialization

    public GameObject FindClosestPlatform()
    {
        GameObject[] gos;
        GameObject[] gos2;
        GameObject[] gos3;
        GameObject[] gos4;
        gos = GameObject.FindGameObjectsWithTag("platform");

        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)

        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }
        return closest;
    }

public IEnumerator wait()
    {
        leftSwipe = true;
        yield return new WaitForSeconds(0.5f);
        leftSwipe = false;
    }



    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {



        closestPlatform = FindClosestPlatform();


        if ((closestPlatform = left) && (leftSwipe = false))
        {
            strike = 1;
        }


        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began)
            {
                startTime = Time.time;
                startPos = touch.position;
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                endTime = Time.time;
                endPos = touch.position;

                swipeDistance = (endPos - startPos).magnitude;
                swipeTime = endTime - startTime;

                if (swipeTime < maxTime && swipeDistance > minSwipeDistance)
                {
                    swipe();
                }
            }

        }

    }


    void swipe()
    {












            Vector2 distance = endPos - startPos;
            if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y))
            {
                Debug.Log("Horizontal Swipe");
                if (distance.x > 0)
                {
                    Debug.Log("Right Swipe");
                }
                if (distance.x < 0)
                {
                    Debug.Log("Left Swipe");

                    StartCoroutine(wait());


                }

            }

            else if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y))
            {
                Debug.Log("Vertical Swipe");
                if (distance.y > 0)
                {
                    Debug.Log("Up Swipe");
                }
                if (distance.y < 0)
                {
                    Debug.Log("Down Swipe");
                }
            }


        }




    }

Aucun commentaire:

Enregistrer un commentaire