Hello,
I’m doing mario jump physics and implementing the input system.
The jump triggers but nothing happens when the code is enabled expect the debug.log(“jump”);
After debugging, I found that the ground doesn’t get detected by the Raycast();
The Clamp works, the camera follows mario correctly.
The main code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Security.Cryptography;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
public class PlayerMovement : MonoBehaviour
{
private Camera cam;
public Rigidbody2D rb;
private Transform player;
public float speed = 8f;
public float maxJumpHeight = 5f;
public float maxJumpTime = 1f;
public float JumpForce_ => 2f * maxJumpHeight / (maxJumpTime / 2f);
public float Gravity_ => -2 * maxJumpHeight / MathF.Pow(maxJumpTime / 2f, 2);
public bool Grounded_ {get; private set;}
public bool Jumping_ {get; private set;}
private UnityEngine.Vector2 vector;
public InputActionReference move;
public InputActionReference jump;
SideScrolling SideScroll;
public void Jump(InputAction.CallbackContext context)
{
if(context.performed)
{
Debug.Log("Jumped");
rb.velocity = new UnityEngine.Vector2(rb.velocity.x, maxJumpHeight);
Jumping_ = true;
}
}
private void Awake()
{
cam = Camera.main;
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
Grounded_ = rb.Raycast(UnityEngine.Vector2.down);
if(Grounded_){
GroundedMovement();
}
ApplyGravity();
vector = move.action.ReadValue<UnityEngine.Vector2>();
Extensions.Raycast();
}
private void ApplyGravity()
{
bool falling = vector.y < 0.005f || Jumping_ == true;
float multiplier = falling ? 2f : 1f;
vector.y += Gravity_ * multiplier * Time.deltaTime;
vector.y = MathF.Max(vector.y, Gravity_ / 2f);
}
private void GroundedMovement()
{
vector.y = Mathf.Max(vector.y, 0f);
Jumping_ = vector.y > 0f;
if(Jumping_ == true){
vector.y = JumpForce_;
Debug.Log("JumpForce_ Transfered");
}
}
private void FixedUpdate()
{
rb.velocity = new UnityEngine.Vector2(vector.x * speed, rb.velocity.y);
UnityEngine.Vector2 position = rb.position;
position += speed * Time.fixedDeltaTime * vector;
UnityEngine.Vector2 leftEdge= cam.ScreenToWorldPoint(UnityEngine.Vector2.zero);
UnityEngine.Vector2 rightEdge = cam.ScreenToWorldPoint(new UnityEngine.Vector2(Screen.width, Screen.height));
position.x = Mathf.Clamp(position.x, leftEdge.x + 0.5f, rightEdge.x);
rb.MovePosition(position);
}
}
Raycast extension:
The following piece of code isn’t used in my example but if I don’t cast it, I get an error:
internal static void Raycast()
{}
Whole extension code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public static class Extensions
{
internal static void Raycast()
{}
private static LayerMask layerMask = LayerMask.GetMask("Default");
public static bool Raycast(this Rigidbody2D rigidbody, Vector2 direction)
{
if (rigidbody.isKinematic){
return false;
}
float radius = 0.5f;
float radiusCast = -0.375f;
RaycastHit2D hit = Physics2D.CircleCast(rigidbody.position, radius, direction, radiusCast, layerMask);
return hit.collider != null && hit.rigidbody != rigidbody;
}
}
I’ll take whatever ideas I can get at this point. Thank you so much for taking the time to read this all.