簡體   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