繁体   English   中英

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

[英]In unity C# how do I make the moveInput = Input.GetAxis("Horizontal"); not read the arrow keys and only detect a and d on the keyboard

好的,所以我正在制作一个 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;
}

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM