简体   繁体   中英

Why my first touch of button doesn't rotate my object?

When i hit q or r my pieces dont rotate the first time, but after the second touch they work perfect. Can someone tell my how to fix it? I tried to modify some variables but it doesn't work and I dont know why, I think its maybe of the boolean variables, but I am not sure. 我用来旋转的碎片

The attach of each piece of the game

Here is my code:

private bool moving;

private float startPosX;
private float startPosY;
private bool heRotado = false;

private float rotZ = 90;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if(moving){
        Vector3 mousePos;
        mousePos = Input.mousePosition;
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);
        this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
    }
    if (moving)
        Rotate();
    
}

// Funcion para rotar las piezas 
// Con la tecla q se gira hacia la izquierda, con la tecla r se gira hacia la derecha
void Rotate(){
    if (Input.GetKey("r") && !heRotado){
        rotZ += 90;
        gameObject.transform.rotation = Quaternion.Euler(0, 0, rotZ);
        heRotado = true;
    }
    if (!Input.GetKey("r") && !Input.GetKey("q")) {
        heRotado = false;
    }

    if (Input.GetKey("q") && !heRotado){
        rotZ -= 90;
        gameObject.transform.rotation = Quaternion.Euler(0, 0, rotZ);
        heRotado = true;
    }
}

// Override
public void OnMouseDown(){
    if (Input.GetMouseButtonDown(0)){
        Vector3 mousePos;
        mousePos = Input.mousePosition;
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);

        startPosX = mousePos.x - this.transform.localPosition.x;
        startPosY = mousePos.y - this.transform.localPosition.y;
        moving = true;
    }

}
// Override
private void OnMouseUp(){
    moving = false;
}

So I fixed by changing this line: private float rotZ = 0;

I am so fool, sorry for the problems I caused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM