Вот код:
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float speed = 10;
private float horizontalInput;
private float verticalInput;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
horizontalInput = -1;
}
if(Input.GetKeyDown(KeyCode.D))
{
horizontalInput = 1;
}
if(Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A))
{
horizontalInput = 0;
}
if(Input.GetKeyDown(KeyCode.S))
{
verticalInput = -1;
}
if(Input.GetKeyDown(KeyCode.W))
{
verticalInput = 1;
}
if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
{
verticalInput = 0;
}
transform.Translate(Vector2.right * horizontalInput * Time.deltaTime * speed);
transform.Translate(Vector2.up * verticalInput * Time.deltaTime * speed);
if(horizontalInput == 1 || horizontalInput == -1 && verticalInput == 1 || verticalInput == -1 )
{
speed = 5f;
}
else
{
speed = 10;
}
}
}
Это начало происходить только тогда, когда я добавил код для исправления ускорения спрайта в диагональных направлениях, но это важная часть кода
Подробнее здесь: https://stackoverflow.com/questions/785 ... -up-direct