Поэтому я хочу, чтобы персонаж не перемещался мгновенно, если входной вектор не совпадает с вектором 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
Как не перемещать игрока мгновенно, если он смотрит не в том же направлении, что и входные данные движения? ⇐ C#
Место общения программистов C#
1761379366
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;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79799360/how-to-not-instantly-move-the-player-if-the-player-isnt-facing-the-same-directi[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия