Я пытался заставить движение моего игрока прекратить, когда клавиша не нажимает, но после нажатия клавиши персонаж никогда не перестает двигаться. Может ли кто -нибудь объяснить мне, что я делаю не так? Я даже пытался добавить больше трения, но безрезультатно. < /P>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class playermovement : MonoBehaviour
{
public float xspeed = 1;
public float yspeed = 1;
public float timer = 0;
public static bool isGrounded = true;
public Rigidbody2D rb;
public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Timer only works while the player is in the air (has jumped)
if (!isGrounded)
{
timer += 1 * Time.deltaTime;
}
//basic player movement
// Lock the rotation so it never changes
transform.rotation = Quaternion.identity;
//d key
if (Input.GetKeyDown(KeyCode.D))
{
Debug.Log("D");
rb.velocity = new Vector2(xspeed, rb.velocity.y);
animator.SetFloat("speed", 0.02f);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rb.velocity = new Vector2(0, rb.velocity.y);
animator.SetFloat("speed", 0f);
animator.SetBool("Left", false);
}
//a
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("A");
rb.velocity = new Vector2(-xspeed, rb.velocity.y);
animator.SetFloat("speed", 0.2f);
animator.SetBool("Left", true);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rb.velocity = new Vector2(0, rb.velocity.y);
animator.SetFloat("speed", 0f);
animator.SetBool("Left", false);
}
//jump
if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
Debug.Log("W");
rb.velocity = new Vector2(rb.velocity.x, yspeed);
isGrounded = false;
animator.SetBool("isJumping", true);
}
if (isGrounded == true)
{
animator.SetBool("isJumping", false);
}
if (isGrounded == true && Input.GetKeyUp(KeyCode.A) && Input.GetKeyUp(KeyCode.D))
{
rb.velocity = new Vector2(rb.velocity.x - rb.velocity.x, yspeed);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... is-pressed