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 `}’
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;
}
}
}
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;
}
}
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;
}
}
}