Таким образом, мой персонаж не может двигаться, пока я прыгаю или я в действии прыжков, в то время как сцена
сбрасывается, поэтому, если сцена сбрасывается, когда я прыгаю с персонажа, не могу двигаться, пожалуйста, помогите
me исправить это, и я предоставлю код, так что спасибо за то, что попытались помочь мне, чтобы все справились.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementSteamDeck : MonoBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 5f;
public float jumpForce = 10f;
[Header("Ground Check Settings")]
// The point from which the check will be performed
public Transform groundCheck;
// Width of the rectangular ground check area (modifiable)
public float checkWidth = 0.5f;
// Fixed height for the ground check area (y-axis remains constant)
private const float fixedCheckHeight = 0.2f;
// Which layers count as ground
public LayerMask groundLayer;
// Coyote time allows a brief jump after leaving a platform
public float coyoteTime = 0.2f;
private float coyoteTimeCounter;
private bool isGrounded;
private bool jumpHeld;
private Rigidbody2D rb;
private Animator anim;
void Start()
{
rb = GetComponent();
anim = GetComponent();
}
void Update()
{
// Update the horizontal movement
Move();
// Update the jump input (A button for jumping on Steam Deck)
jumpHeld = Input.GetButton("Jump");
// Update ground check via BoxCast
CheckGround();
// Update animations
UpdateAnimations();
}
void FixedUpdate()
{
// Allow jumping if jump is held and player is grounded or within coyote time
if (jumpHeld && (isGrounded || coyoteTimeCounter > 0))
{
Jump();
}
}
void Move()
{
// Get joystick input for Steam Deck
float moveInput = Input.GetAxisRaw("Horizontal"); // Using raw input for instant
response
// Check if both A and D keys or both Left and Right arrow keys are pressed
bool aPressed = Input.GetKey(KeyCode.A);
bool dPressed = Input.GetKey(KeyCode.D);
bool leftArrowPressed = Input.GetKey(KeyCode.LeftArrow);
bool rightArrowPressed = Input.GetKey(KeyCode.RightArrow);
if ((aPressed && dPressed) || (leftArrowPressed && rightArrowPressed))
{
moveInput = 0; // Prevent movement if both keys are pressed
}
else if (aPressed || leftArrowPressed)
{
moveInput = -1; // Move left when only A or Left Arrow is pressed
}
else if (dPressed || rightArrowPressed)
{
moveInput = 1; // Move right when only D or Right Arrow is pressed
}
// Apply movement or stop sliding
if (moveInput == 0)
{
rb.velocity = new Vector2(0, rb.velocity.y); // Stop horizontal movement to
prevent sliding
}
else
{
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y); // Move
player
transform.localScale = new Vector3(Mathf.Sign(moveInput), 1, 1); // Flip
character to face direction of movement
}
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
// After jumping, reset ground-related flags
isGrounded = false;
coyoteTimeCounter = 0f;
}
void CheckGround()
{
// Define the size of the box for checking
Vector2 boxSize = new Vector2(checkWidth, fixedCheckHeight);
// Use a BoxCast downward with zero distance to get collision info including the
surface normal
RaycastHit2D hit = Physics2D.BoxCast(groundCheck.position, boxSize, 0f,
Vector2.down, 0f, groundLayer);
// Only consider the surface as ground if the normal is mostly upward
if (hit.collider != null && hit.normal.y > 0.7f)
{
isGrounded = true;
coyoteTimeCounter = coyoteTime; // Reset coyote time when on ground
}
else
{
isGrounded = false;
coyoteTimeCounter -= Time.deltaTime;
}
}
void UpdateAnimations()
{
anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
// The character is considered jumping if not grounded and coyote time has elapsed
anim.SetBool("isJumping", !isGrounded && coyoteTimeCounter
Подробнее здесь: https://stackoverflow.com/questions/795 ... p-unity-2d
Мой персонаж не может двигаться во время прыжка, когда сцена сбрасывает C# Unity 2d ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Мой персонаж не может двигаться во время прыжка, когда сцена сбрасывает C# Unity 2d
Anonymous » » в форуме C# - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Персонаж застревает при столкновении с возвышением и стенами — невозможно двигаться в сторону
Anonymous » » в форуме C# - 0 Ответы
- 29 Просмотры
-
Последнее сообщение Anonymous
-