Unity: Spieler-Sprunglogik funktioniert nicht richtig – isGrounded ist immer wahr, auch wenn es nicht am Boden istC#

Ein Treffpunkt für C#-Programmierer
Guest
 Unity: Spieler-Sprunglogik funktioniert nicht richtig – isGrounded ist immer wahr, auch wenn es nicht am Boden ist

Post by Guest »

Ich arbeite an einem Unity 3D-Projekt und habe ein Problem mit dem Springen von Spielern. Der Spieler kann nur einmal springen und nach dem ersten Sprung nicht mehr springen, selbst nachdem er wieder auf dem Boden gelandet ist. Darüber hinaus gibt die isGrounded-Bedingung immer „true“ zurück, auch wenn der Spieler nicht am Boden ist.
Ich verwende einen CharacterController und Physics.CheckSphere für die Bodenerkennung. Unten ist der Code, den ich verwende:

Code: Select all

using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float speed = 10f;         // Movement speed
public float jumpForce = 10f;     // Jump force
private bool canJump = true;      // Flag to check if the player can jump

private CharacterController controller;
private Vector3 velocity;        // Used to apply gravity and jump force

public Transform groundCheck;     // Position for ground detection
public float groundDistance = 0.3f;  // Radius of ground check sphere (adjustable)
public LayerMask groundMask;      // Layer mask for ground detection

void Start()
{
controller = GetComponent();
}

void Update()
{
// Ground check using Physics.CheckSphere
bool isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

// Debugging: Output whether the player is grounded and the position of groundCheck
Debug.Log("Is Grounded: " + isGrounded + " | GroundCheck Position: " + groundCheck.position);

// When grounded, reset downward velocity and allow jumping
if (isGrounded)
{
if (velocity.y < 0)
{
velocity.y = -2f;  // Small downward force to keep player grounded
}
canJump = true;    // Allow jumping when grounded
}
else
{
canJump = false;  // Disable jumping when not grounded
}

// Player movement
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");

Vector3 move = transform.right * moveX + transform.forward * moveZ;
controller.Move(move * speed * Time.deltaTime);

// Jumping logic (only allow jump when grounded)
if (Input.GetButtonDown("Jump") && canJump)
{
// Apply jump force
velocity.y = Mathf.Sqrt(jumpForce * -2f * Physics.gravity.y);

// Disable jumping until grounded again
canJump = false;
}

// Apply gravity to simulate falling
velocity.y += Physics.gravity.y * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}

void OnCollisionEnter(Collision collision)
{
// If the player collides with the ground, set canJump to true
if (collision.gameObject.CompareTag("Ground"))
{
canJump = true;  // Allow jumping again when touching the ground
}
}

// Visualize the ground check area
void OnDrawGizmos()
{
if (groundCheck != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(groundCheck.position, groundDistance);  // Visualize the ground check area
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post