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:
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
}
}
}
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]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
// 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");
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 } } }
Ich habe Browser mit so vielen Threads und Beiträgen zum Thema „Keystore-Datei existiert, ist aber leer“, aber ich kann in Unity keine wirklich passende Frage und Antwort zu meiner finden.
Ich versuche, die Anforderungsparams im Thymeleaf -Code mithilfe von Param Variable abzurufen. Hier ist die Dokumentation für Param:
so ich habe diesen Code geschrieben. stat+1
Ich habe ein Div-Box (Fluss) mit einer variablen Menge an Inhalten im Inhalt. Tag?
Wenn ich es für die gesamte Seite tun wollte, würde ich das. 564px (der Div ist auf 500 als Höhe eingestellt) und...
Ich habe einige kleine Programme in Java gemacht. Ich weiß das, wenn ich schreibe while(true); Das Programm friert in dieser Schleife ein. Wenn der Code so ist:
Ich habe kürzlich ein bisschen Code in meinem Spieler entwickelt, wodurch der Spieler auf der Höhe der Zeit, in der die Jump -Taste abgehalten wird, auf einer Höhe springen lässt. Wenn Sie jedoch auf...