Need help with this character controller(jump doesn't work),I'm having trouble with a character controller
I just started learning unity and I have basic programming knowledge. I don't know what is wrong but when ran no errors appear in the console. The horizontal movement works just fine but when the space bar is pressed nothing happens. This is the code below:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Controller : MonoBehaviour {
private bool isJump = false;
public Rigidbody2D rb;
public int jumpHeight;
Vector2 move;
public float speed;
void Update()
{
move.x = Input.GetAxisRaw("Horizontal");
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
isJump = false;
}
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);
if (Input.GetKey(KeyCode.Space) && isJump == false)
{
Debug.Log("jump");
isJump = true;
rb.velocity = new Vector2(0, jumpHeight);
}
}
},I'm a beginner who has just started to learn unity and I have basic programming knowledge. But I have no idea why this is not working. No errors pop up in the console once the games begins. The only problem is the jump. I don't know what is no working the rb.velocity or the OnCollisionEnter2D. This is the code I'm using below:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Controller : MonoBehaviour {
private bool isJump = false;
public Rigidbody2D rb;
public int jumpHeight;
Vector2 move;
public float speed;
void Update()
{
move.x = Input.GetAxisRaw("Horizontal");
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
isJump = false;
}
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);
if (Input.GetKey(KeyCode.Space) && isJump == false)
{
Debug.Log("jump");
isJump = true;
rb.velocity = new Vector2(0, jumpHeight);
}
}
}