- Home /
change multiple jump to single jump
I am doing the flappy bird project and now I am finished and I want to change the flying into a single jump. I have also watched at a video of a single jump example but it says: Assets\Script\Bird.cs(28,53): error CS0103: The name 'jumpVelocity' does not exist in the current context and: Assets\Script\Bird.cs(29,33): error CS0019: Operator '*' cannot be applied to operands of type 'Vector2' and 'void'
my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bird : MonoBehaviour {
public float upForce = 200f;
private bool isDead = false;
private Rigidbody2D rb2d;
private Animator anim;
private Rigidbody2D rigidbody2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (isDead == false)
{
if (Input.GetMouseButtonDown(0))
{
rigidbody2d.velocity = Vector2.up * jumpVelocity;
rb2d.velocity = Vector2.up *
rb2d.AddForce(new Vector2(0, upForce));
anim.SetTrigger("Flap");
}
}
}
void OnCollisionEnter2D()
{
rb2d.velocity = Vector2.zero;
isDead = true;
anim.SetTrigger("Die");
GameController.instance.BirdDied();
}
}
Answer by pauldarius98 · Mar 02, 2021 at 10:07 AM
I guess you are new to programming so i want to tell you that the compiler is your friend and it will point out what you are doing wrong. When you have errors, you can search what that error means and usually you will see what you did wrong in your code. Now, let's take a look at the errors:
'jumpVelocity' does not exist in the current context means that we used a variable named jumpVelocity but we never declared it. This can be fixed very easy by declaring it at the top of our class (or anywhere but is good to declare the variables at the top of your classes) like this:
public float upForce = 200f; private bool isDead = false; private Rigidbody2D rb2d; private Animator anim; private Rigidbody2D rigidbody2d; private float jumpVelocity = 300f; //Change 300 with whatever value you want ...
Operator '' cannot be applied to operands of type 'Vector2' and 'void': It means that we tried to multiply a Vector2 with void (you see that you wrote nothing after the ?. From your context, i guess that you wanted to multiply it with the upForce like this:
rb2d.velocity = Vector2.up * upForce;
Do this changes and see if your code compiles now
Your answer
Follow this Question
Related Questions
2d rigidbody jump not working 1 Answer
How can I limit jump height in unity? 3 Answers
I cant make my character jump 1 Answer