- Home /
GetKeyDown(KeyCode.Space) not working!?
Hey guys, as beginner in scripting, I am trying to make movements for my "character". Everytime I press spacebar or tap on my screen of my phone, it does not work in the simulator. My goal is to make my "character" go down which means that the velocity needs to be negative. I have set the gravity of the object a positive number. What did I do wrong?
bool didfly = false;
void update(){
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didfly = true;
}
}
Thanks in advance!
Answer by tw1st3d · May 21, 2014 at 09:33 PM
void update()
Should most certainly read:
void Update()
Answer by Hachley · May 21, 2014 at 09:07 PM
What exactly is not working with the code you posted? To test out if it works just change it to:
void update(){
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
Debug.Log("test"); //this
}
But I suspect what you are asking is how to make your "character" go down, which I suppose you have a code made for it already, because "didfly" bool just ain't gonna cut it. Does your going-down code works or not? If your question on how to make your character go down, then you can just have the rigidbody on your character be affected by gravity, and just make him be Kinematic whenever didfly is false:
//add this to the update void with the rest you've got
if(!didfly) { //if didfly is false, then make char Kinematic
rigidbody.isKinematic = true;
} else {
rigidbody.isKinematic = false;
}
That's about it.
Thank you for your reply, Hachley. I have no idea what's not working. I have tried debug.log but nothing pops out in the console. Same goes to kinematic
public class Balloon$$anonymous$$ovement : $$anonymous$$onoBehaviour {
//didflap = didfly
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 flyVelocity;
public float maxSpeed = 5f;
bool didfly = false;
// Use this for initialization
void Start () {
}
//Graphics and Input updates
//Get$$anonymous$$ouseButtonDown(0)=left button
void update(){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown(0) ) {
Debug.Log("test"); //this
//add this to the update void with the rest you've got
if(!didfly) { //if didfly is false, then make char $$anonymous$$inematic
rigidbody.is$$anonymous$$inematic = true;
} else {
rigidbody.is$$anonymous$$inematic = false;
}
}
}
//Physics engine update
void FixedUpdate () {
velocity += gravity * Time.deltaTime;
if (didfly == true) {
didfly = true;
velocity += flyVelocity;
}
velocity = Vector3.Clamp$$anonymous$$agnitude (velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}
Answer by temstindoesunity · May 21, 2014 at 10:03 PM
Try if(Input.GetKeyDown("space")||Input.GetMouseButtonDown(0) ) { didfly = true; } }
Your answer
Follow this Question
Related Questions
Too many keys?! 1 Answer
Trying to make character move like frogger 0 Answers
Simple Quick Question 2 Answers
Problem With else if statement 1 Answer
Trouble getting key down to work 1 Answer