- Home /
Keycode issues, newb(Sensationalist title removed)
I am having a huge problem with getting my code to work right. I want to move left right up and down, but I keep getting errors with my code. This is my very first game made and I just started coding, but I think I got most of it right. CODE(BELOW)
#pragma strict
var moveup :KeyCode;
var movedown :KeyCode;
var moveleft :KeyCode;
var moveright :KeyCode;
var speed : float = 10;
function Update ()
{
if (Input.GetKey(moveup))
{
rigidbody2D.velocity.y = speed;
}
else if (Input.GetKey(movedown))
{
rigidbody2D.velocity.y = speed *-1;
}
else
{
rigidbody2D.velocity.y =0;
}
if (Input.GetKey(moveleft))
{
rigidbody2D.velocity.x = speed *-1;
}
else if (Input.GetKey(moveright)).x = speed;
{
else
{
rigidbody2D.velocity.x =0;
}
};
Ah LOL man, you dont need to write out the line numbers manually but well done for the effort. It's good that you were considerate enough to put them in.
Highlight all your code and click the 101010 button! :D
Oh yes, please post the errors.
Try adding keycode directly to Get$$anonymous$$ey.
Input.Get$$anonymous$$ey($$anonymous$$eyCode.G) //Letter G
Using GetButton will access custom buttons from Input manager,
Agreed. Tell us exactly what is happening incorrectly. Also, do you ever assign moveup, etc.?
Uldiem I do assign the move up and move down keys within the script code once it's implemented into the character. I will try these specific key binds though. I was using the same move script from when I tried recreating Pong lol
Answer by Landern · Nov 20, 2014 at 07:11 AM
Without the ACTUAL error, this becomes difficult. but off the bat, your if statements are malformed, you have a variable being set in an else if condition..., adjust to:
#pragma strict
var moveup :KeyCode;
var movedown :KeyCode;
var moveleft :KeyCode;
var moveright :KeyCode;
var speed : float = 10;
function Update ()
{
if (Input.GetKey(moveup))
{
rigidbody2D.velocity.y = speed;
}
else if (Input.GetKey(movedown))
{
rigidbody2D.velocity.y = speed *-1;
}
else
{
rigidbody2D.velocity.y =0;
}
if (Input.GetKey(moveleft))
{
rigidbody2D.velocity.x = speed *-1;
}
else if (Input.GetKey(moveright))
{
rigidbody2D.velocity.x = speed;
}
else
{
rigidbody2D.velocity.x =0;
}
};
Your answer
Follow this Question
Related Questions
How long should it take me to make my game 2 Answers
How do I single out one object amongst a group of objects with the same tag? 3 Answers
I don't understand Vector2 + Vector3 3 Answers
Parenting Objects 1 Answer
Why isn't my word generator working? 1 Answer