Мой лихой персонаж не перемещается, но вся отладка работаетC#

Место общения программистов C#
Ответить
Anonymous
 Мой лихой персонаж не перемещается, но вся отладка работает

Сообщение Anonymous »

Код: Выделить всё

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;

public class PlayerController : MonoBehaviour
{
// Input and movement variables
private Vector2 direction;
private Rigidbody2D rb;
private PlayerControls controls;

// Dash variables
private float DashTimeLeft;
private float LastImageExp;
private float LastDash = -100;
private bool isDashing;

// Player movement parameters
public float speed;
public Animator animator;
public Transform weaponParent;

// Dash parameters
public float DashTime;
public float DashSpeed;
public float DistanceBetweenImages;
public float DashCooldown;

private void Awake()
{
// Enable input controls and touch support
controls = new PlayerControls();
controls.Enable();
EnhancedTouchSupport.Enable();
}

private void OnEnable()
{
// Register input callbacks
controls.Player.Move.performed += ctx => direction = ctx.ReadValue();
controls.Player.Move.canceled += ctx => direction = Vector2.zero;
controls.Player.Dashing.performed += ctx => Dash();
}

private void OnDisable()
{
// Unregister input callbacks on disable
controls.Player.Move.performed -= ctx => direction = ctx.ReadValue();
controls.Player.Move.canceled -= ctx => direction = Vector2.zero;
}

private void Start()
{
// Initialize Rigidbody and Animator components
rb = GetComponent();
animator = GetComponent();
}

private void FixedUpdate()
{
// Normalize input direction
direction.Normalize();

// Set animator parameters
animator.SetFloat("Horizontal", direction.x);
animator.SetFloat("Vertical", direction.y);
animator.SetFloat("Speed", direction.magnitude);

// Move character, set blend tree, rotate weapon, and check dash
MoveCharacter();
SetBlendTree();
RotateWeapon();
CheckDash();
}

private void MoveCharacter()
{
// Move character based on input direction unless dashing
if (!isDashing)
{
rb.velocity = direction * speed;
}
else
{
rb.velocity = Vector2.zero; // Stop regular movement during dash
}
}

private void SetBlendTree()
{
// Set animator parameters for blend tree based on input direction
if (direction.magnitude > 0)
{
animator.SetInteger("LastDirection", GetDirectionCode());
animator.SetBool("IsWalking", true);
}
else
{
animator.SetBool("IsWalking", false);
}
}

private int GetDirectionCode()
{
// Determine and return the direction code based on input angle
float x = direction.x;
float y = direction.y;
float northAngleThreshold = Mathf.PI / 3f;

float angle = Mathf.Atan2(y, x);

if (angle > -northAngleThreshold && angle < northAngleThreshold)
{
return 3; // 3 - North
}
else if (angle > northAngleThreshold && angle < Mathf.PI - northAngleThreshold)
{
return 1; // 1 - East
}
else if (angle < -northAngleThreshold && angle > -Mathf.PI + northAngleThreshold)
{
return 2; // 2 - West
}
else
{
return 4; // 4 - South
}
}

private void RotateWeapon()
{
// Rotate weapon based on input direction
if (weaponParent != null && direction.magnitude >  0)
{
float angle = Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg;
weaponParent.rotation = Quaternion.Euler(0f, 0f, angle);
}
}

public void Dash()
{
// Trigger dash if cooldown is over
if (Time.time >= (LastDash + DashCooldown))
{
AttemptToDash();
Debug.Log("Dash button pressed");
}
else
{
isDashing = false;
}
}

private void AttemptToDash()
{
// Start dash, set dash parameters, and create afterimage
isDashing = true;
DashTimeLeft = DashTime;
LastDash = Time.time;

PlayerAfterImagePool.Instance.GetFromPool();
LastImageExp = transform.position.x;
}

private void CheckDash()
{
// Check and perform dash if active
Debug.Log("Checking Dash");

if (isDashing)
{
Debug.Log("Dashing");

if (DashTimeLeft > 0)
{
// Move character during dash and create afterimages
rb.velocity = new Vector2(DashSpeed * direction.x, rb.velocity.y);
DashTimeLeft -= Time.deltaTime;

rb.MovePosition(rb.position + new Vector2(DashSpeed * direction.x, 0) * Time.deltaTime);

if (Mathf.Abs(transform.position.x - LastImageExp) > DistanceBetweenImages)
{
PlayerAfterImagePool.Instance.GetFromPool();
LastImageExp = transform.position.x;
}
}

if (DashTimeLeft 

Подробнее здесь: [url]https://stackoverflow.com/questions/77835562/my-dashing-doesnt-move-character-but-all-debug-works[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»