首页 > 解决方案 > 在统一 C# 中,我如何使 moveInput = Input.GetAxis("Horizo​​ntal"); 不读取方向键,只检测键盘上的 a 和 d

问题描述

好的,所以我正在制作一个 Metroidvania,我在代码中使用了一些简单的输入,但我想使用箭头键来获得特殊能力,而使用 wasd 键来移动。当我使用我的代码时,它会读取 wasd 和箭头键。如何让它只检测wasd而不检测箭头键?请帮忙!这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveInput;

private Rigidbody2D rb;

private bool facingRight = true;

private bool isGrounded;
public Transform GroundCheck;
public float checkRadius;
public LayerMask whatIsground;

private int extraJumps;
public int extraJumpsValue;

void Start()
{
    extraJumps = extraJumpsValue;
    rb = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{


    moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

    if(facingRight == false && moveInput > 0)
    {
        Flip();
    }else if(facingRight == true && moveInput < 0)
    {
        Flip();
    }
}

void Update()
{
    isGrounded = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, whatIsground);

    if (isGrounded == true)
    {
        extraJumps = extraJumpsValue;
    }

    if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
    {
        rb.velocity = Vector2.up * jumpforce;
        extraJumps--;
    }else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
    {
        rb.velocity = Vector2.up * jumpforce;
    }
}

void Flip()
{
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
}

}

标签: c#unity3d

解决方案


这取决于您使用的输入系统。假设您使用的是旧系统,请转到编辑 > 项目设置 > 输入。找到“水平”轴并更改您想要的任何键或 alt 键。


推荐阅读