- Home /
Why is my float not fitting into my AddForce script?
I'm really new to Unity, and I'm trying to make a 2d platform script with the little knowledge I have. I made a script for jumping, but there's a problem. Here's my code:
public float jumpHeight;
bool jumping = false;
public Rigidbody2D rb;
void Update()
{
if (Input.GetKey("w"))
{
if (jumping == false)
{
rb.AddForce(0, jumpHeight);
jumping = true;
}
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.tag == "Platform")
{
jumping = false;
}
}
When I try this, I get an error message that tells me it can't convert a float to a Vector2, even though I put (0, jumpHeight) and not (jumpHeight).
My workaround was this:
public Vector2 jumpHeight;
bool jumping = false;
public Rigidbody2D rb;
void Update()
{
if (Input.GetKey("w"))
{
if (jumping == false)
{
rb.AddForce(jumpHeight);
jumping = true;
}
}
}
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.tag == "Platform")
{
jumping = false;
}
}
That works, but it isn't very practical, and I'm sure there's a much better way to do it.
I followed a Brackeys tutorial (for a 3d project) and he said to use the float inside of the vector to move left and right, and that works without a problem. What's the difference here?
Answer by Zoedingl · Jun 21, 2020 at 01:40 PM
Instead of (0, jumpHeight) use new Vector2(0, jumpHeight).
Your answer
Follow this Question
Related Questions
Cannot implicitly convert type 'float' to 'bool' 3 Answers
BCE0023 error 2 Answers
IComparable error due to float 1 Answer
creating a Vector2 using transform.position not working - c# 1 Answer