- Home /
Question by
xpaintall · 6 days ago ·
rigidbody.addforce
When adding force to the y-axis gameobject it doesn't want to stop
So basically I want to make my character jump (spoiler alert: it doesn't work) and when I tried to get near my jumppad (prefab) the force that was supposed to add itself once does it an infinite amount of times.
I watched a couple of youtube tutorials on how to make movement and since I'm very new to unity I have no idea how to fix this:
2 jumping classes made for jumping:
public class jumping : MonoBehaviour
{
public float jumpingHeight = 10f;
public bool isOnGround = false;
void FixedUpdate()
{
Jump();
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && isOnGround == true)
{
if (gameObject.CompareTag("Player"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpingHeight), ForceMode2D.Impulse);
}
}
}
}
public class groundcheck : MonoBehaviour
{
GameObject Player;
private void Start()
{
Player = gameObject.transform.parent.gameObject;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag == "Floor")
{
Player.GetComponent<jumping>().isOnGround = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Floor")
{
Player.GetComponent<jumping>().isOnGround = false;
}
}
}
The jump pad: public class bounce : MonoBehaviour {
public float bounceAmount;
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Player")) {
collision.gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 20f), ForceMode2D.Impulse);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Adding Force to Rigidbody2D in C# 1 Answer
Player movement not working 1 Answer
Character on platform move slowly 1 Answer
how to limit horizontal input when jumping 1 Answer
Adding force at an angle 2D,Adding force at an angle 1 Answer