- Home /
Question by
8673589 · Jun 28, 2015 at 06:44 PM ·
transformpositionif-statements
Sprite changing position when button pressed
If I alter around with a few of the jump values - true/false, the sprite will move position correctly but only once. I am trying to make it so that whenever the button is pressed the sprite will move to the opposing position. Here is my code so far. I have position as 0, 1.85 and position2 as 0, -1.85.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
bool jump = false;
public Vector2 position = new Vector2(0, 0);
public Vector2 position2 = new Vector2(0, 0);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) {
if(jump == false){
jump = true;
transform.position = position;
}
if(jump == true){
jump = false;
transform.position = position2;
}
}
}
}
Comment
Answer by jenci1990 · Jun 29, 2015 at 05:01 PM
Use 'if' 'else':
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) {
if(jump == false){
jump = true;
transform.position = position;
}else {
jump = false;
transform.position = position2;
}
}
}
Your answer
Follow this Question
Related Questions
If gameobject moves do this 1 Answer
Moving object with transform.position ignore other objects even if they collided 1 Answer
Need enemy to follow only active character 0 Answers
Drawing a (debug) line between anchored UI element and mouse? 0 Answers
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers