Как не перемещать игрока мгновенно, если он смотрит не в том же направлении, что и входные данные движения?C#

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

Сообщение Anonymous »

Поэтому я хочу, чтобы персонаж не перемещался мгновенно, если входной вектор не совпадает с вектором FacingDirection. Вместо этого я хочу, чтобы персонаж просто изменил направление своего обращения в соответствии с FaceDirection. Я пробовал устранить неполадки с помощью GPT, но это не помогло.
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public class NewMonoBehaviourScript : MonoBehaviour
{
public float moveSpeed;
public LayerMask solidObjectsLayer;

private bool isMoving;
private Vector2 input;
private Vector2 facingDirection;

//animator initialization
private Animator animator;
private void Awake()
{
animator = GetComponent();
}

private void Update()
{
//only checks if the player is not moving
if (!isMoving)
{
//getting the input
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");

//makes sure there's no diagonal movement
if (input.x != 0) input.y = 0;

//checks again for input
if (input != Vector2.zero)
{
//detector for animation
animator.SetFloat("moveX", input.x);
animator.SetFloat("moveY", input.y);

//checks if there's input AND isMoving is true
if (input != Vector2.zero && !isMoving)
{
//if facingDir = input then the char moves
if (input == facingDirection)
{
//targetPos is updated
var targetPos = transform.position;
targetPos.x += input.x;
targetPos.y += input.y;

//moves the player
if (IsWalkable(targetPos))
StartCoroutine(Move(targetPos));
}
/*when facingDir != input then the character only change direction
and doesn't move (the character already changes facing direction
with the previous animator.SetFloat)*/
else
{
facingDirection = input;
}
}

}
}
animator.SetBool("isMoving", isMoving);
}

IEnumerator Move(Vector3 targetPos)
{
isMoving = true;

while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;

isMoving = false;
}

private bool IsWalkable(Vector3 targetPos)
{
if (Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectsLayer) != null)
{
return false;
}

return true;
}
}



Подробнее здесь: https://stackoverflow.com/questions/797 ... me-directi
Ответить

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

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

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

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

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