mardi 30 août 2016

if-else condition not being checked, FixedUpdate method Unity3D

void FixedUpdate () {

        Vector3 pos = GameObject.FindWithTag("Player").transform.position;
        Vector3 newPos = GameObject.FindWithTag("Player").transform.position;

        moveDirection = Vector3.zero;

        if(pos.x == 0f){
            if (Input.GetKey (KeyCode.A)) {
                newPos.x = -1.3f;
                transform.position = Vector3.Slerp (pos, newPos, movespeed);
            } 
            else if (Input.GetKey (KeyCode.D)) {
                newPos.x = 1.3f;
                transform.position = Vector3.Slerp (pos, newPos, movespeed);
            }
        }
        else if(pos.x == 1.3f){
            if(Input.GetKey(KeyCode.A)){
                newPos.x = 0f;
                transform.position = Vector3.Slerp (pos, newPos, movespeed);
            }
        }
        else if(pos.x == -1.3f){
            if(Input.GetKey(KeyCode.D)){
                newPos.x = 0f;
                transform.position = Vector3.Slerp (pos, newPos, movespeed);
            }
        }

        moveDirection.z = movespeed;    
        controller.Move (moveDirection * Time.deltaTime);
    }

i am using this code to move my player between three lanes. the game starts at the middle lane which is at 0 position of x, the right lane is at 1.3 of x and the left lane is at -1.3 of x. when the game starts it gets pos.x == 0f to be true and the A and D controls work and moves the player to the left or the right lanes according to input but once the player is at 1.3 or -1.3 the other two else-if conditions are not being checked i guess so i can't go back to the middle lane. i don't know why its happening. this code gives no error and it should work. any idea?

Aucun commentaire:

Enregistrer un commentaire