C#: How do you make a 2D game object jump?
I looked up a tutorial on youtube, and got this code from it:
using System.Collections; using UnityEngine;
public class JumpScript : MonoBehaviour {
public float jumpHeight;
public bool isJumping = false;
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.Space)){
if(isJumping == false)
{
Rigidbody2D.AddForce(Vector2.up * jumpHeight);
isJumping = true;
}
}
void OnCollisionEnter2D(Collision col)* †
if(col.GameObject.tag == "ground")
{
IsJumping = false;
}
}
}†† However, it keeps giving me errors. Does anyone know what to do? List of errors: *"Keyword 'void' cannot be used in this context †"Unexpected symbol (', expecting
,', ;', or
='" ††Unexpected symbol `}'
Answer by UnityCoach · Feb 23, 2017 at 08:59 PM
you have a method (OnCollisionEnter2D) within the body of another method (Update), and a few other typos.
Here :
public class JumpScript : MonoBehaviour
{
public float jumpHeight;
private bool isJumping = false; // this doesn't need to be public
private RigidBody2D _rigidBody2D;
private void Awake ()
{
_rigidBody2D = GetComponent<RigidBody2D>();
}
private void Update ()
{
if (Input.GetKeyDown (KeyCode.Space) && !isJumping) // both conditions can be in the same branch
{
_rigidBody2D.AddForce(Vector2.up * jumpHeight); // you need a reference to the RigidBody2D component
isJumping = true;
}
}
private void OnCollisionEnter2D (Collision col)
{
if (col.gameObject.tag == "ground") // GameObject is a type, gameObject is the property
{
isJumping = false;
}
}
}
the only problem is when i tried doing that it let me jump on the side of walls
Answer by AevinGames · Feb 23, 2017 at 09:19 PM
Just looking at it there, maybe this is your problem
void OnCollisionEnter2D(Collision col) {
if(col.GameObject.tag == "ground")
{
IsJumping = false;
}
}
instead of
void OnCollisionEnter2D(Collision col)* †
if(col.GameObject.tag == "ground")
{
IsJumping = false;
}
}
When I do the addForce my frog just flies off the map... Any solution to that?
Answer by stym78899 · Jul 10, 2020 at 10:15 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class fghfgdf : MonoBehaviour { public GameObject platform; public Rigidbody2D rb;
public bool isJumping = false;
public float jumpPower;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown (KeyCode.Space))
{
if (isJumping == false)
{
rb.AddForce(Vector2.up * jumpPower);
isJumping = true;
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (platform.tag == "ground")
{
isJumping = false;
}
}
},using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Jump : MonoBehaviour { public GameObject platform; public Rigidbody2D rb;
public bool isJumping = false;
public float jumpPower;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown (KeyCode.Space))
{
if (isJumping == false)
{
rb.AddForce(Vector2.up * jumpPower);
isJumping = true;
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (platform.tag == "ground")
{
isJumping = false;
}
}
}
Your answer
Follow this Question
Related Questions
Having a couple of problems regarding 2D Movement. 1 Answer
How do I get a reference to a script that is on another gameobject 0 Answers
Is there a way I can make one script for all my buttons in my game? 1 Answer
Jump Script 2 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer