Quantcast
Viewing all articles
Browse latest Browse all 2891

Down movement: Why can't you subtract transform.position.y from 0?

I'm new to Unity and I'm creating a basic program which lets the user move a sprite with the arrow keys. Left/right movement is OK. Now, I want the user to be able to move the sprite down, but if they are not pressing the down key anymore the sprite should slowly move back up to (whatever the x position is, 0). The sprite should always be at or below y = 0. So far I have the script below, but there is an error. int i = currentY; returns CS0266 Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) Does anyone know why this happens and how to fix this? Thank you! if (Input.GetKey(KeyCode.DownArrow)) { if (transform.position.y >= maximumY) { transform.Translate(new Vector2(0, -1)); } } else { if (transform.position.y < 0) { float currentY = 0.0f - transform.position.y; for (int i = currentY; i < 0; i--) { transform.Translate(new Vector2(transform.position.x, 1)); } } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles