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!
It's unknown because you haven't defined it anywhere. You need to define variables before you can use them.
You need to use datatypes instead 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);"
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.
public float Speed;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up* Speed);
}
}
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);
You need to use datatypes instead 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);"
– TonicMind