- Home /
jumping error!?
i am making a 2d platformer that involves jumping but when i put my code in it gives me this error: Unknown identifier moveDirection. but i know it should a known identifier because it's highlighted orange!
my script:
var jumpSpeed = 5.0;
if(Input.GetKeyDown("UpArrow"))
{
moveDirection.y = jumpSpeed;
}
p.s. please help!
Answer by Eric5h5 · Aug 19, 2010 at 07:20 PM
It's unknown because you haven't defined it anywhere. You need to define variables before you can use them.
what do i need to put in after i type var moveDirection = ? if i put a number it comes up with an error.please comment back! thanks!
You need to use datatypes ins$$anonymous$$d of the generic 'var'. It's good practice and will help you hone your skills. But you said you need a direction so you ought to write "var moveDirection = new vector3(x, y,z);"
Answer by Statement · Dec 10, 2010 at 09:05 PM
Try this.
var jumpSpeed = 5.0; var moveDirection : Vector3;
if(Input.GetKeyDown("UpArrow")) { moveDirection.y = jumpSpeed; }
Answer by FallingRocketGames · Nov 23, 2018 at 02:00 AM
I believe you should be approaching this issue in a different way, I recommend you to use AddForce and study a little about the Rigidbody2d component in your Player, also check about ForceMode like Impulse. https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html https://docs.unity3d.com/ScriptReference/ForceMode2D.html
public float Speed;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up* Speed);
}
}
Answer by LearningWhileScrewing · Nov 23, 2018 at 04:11 AM
var jumpSpeed = 5.0; var moveDirection Vector2; if(Input.GetKeyDown("UpArrow"))
{
moveDirection.y = jumpSpeed;
} //this is javascript, so not good at javascript // lower 1 is c# //============ public float jumpSpeed; public Rigidbody2D rb2d; void Start() { rb2d = GetComponent(); }
void Update() {
if (Input.GetKey("up"))
{
Vector2 movement = new Vector2(0, 5);
if(movement != Vector2.zero) {
rb2d.AddForce(movement * jumpSpeed);
} } }
Your answer
Follow this Question
Related Questions
Can't compile script from Scripting Tutorial (translated to Boo) 3 Answers
I have an error while building the game 1 Answer
I got a major question 1 Answer
Unexpected Token: Collider. 1 Answer
What does BCE0004: Ambiguous reference error mean? 3 Answers