I tried to make a Jump sript for a 2D platformer but it didnt work. So I made a Jump script for a 3D box and it work fine. Than i changed all the 3D parts (Rigidbody, Collider) to 2D and used a 2D character but he didnt jump. The force is applied but the object didnt move. I also made the smae character with 3D Collider and 3D Rigidbody and it worked. Are there major differences between the 2D and 3D Rigidbody?
The script for the 2D Jump:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public float jumpforce;
public float speed;
public float gravity;
float relativeGravity;
bool isGrounded = false;
Rigidbody2D rbody;
// Use this for initialization
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
private void OnTriggerStay2D(Collider2D collision)
{
isGrounded = true;
Debug.Log("isGrounded: " + isGrounded);
}
private void FixedUpdate()
{
//Gravity
if (rbody.velocity.y < 0)
{
relativeGravity = gravity * 2;
}
else
{
relativeGravity = gravity;
}
rbody.AddForce(Vector2.down * relativeGravity);
//Jump
if (Input.GetButton("Jump") && isGrounded)
{
rbody.AddForce(new Vector2(0, 1) * jumpforce,ForceMode2D.Impulse);
isGrounded = false;
Debug.Log("Jumped");
}
//Movement
rbody.MovePosition(new Vector2(rbody.position.x + Input.GetAxis("Horizontal") * speed, rbody.position.y));
Debug.Log("velocity.y: " + rbody.velocity.y);
}
}