Input.GetButtonDown("")的用法

本文介绍了Unity中Input.GetButtonDown方法的使用技巧,包括如何在Input Manager中设置按键映射,以及该方法与Input.GetKeyDown和Input.GetMouseButtonDown的区别。适合Unity初学者及需要了解输入控制的开发者。

Input.GetButtonDown("")的用法

转载 2015-06-07 18:07:45
标签: it
pic01
pic02
pic03

GetButtonDown的用法:
1. 通过 Edit-Project settings-Input打开InputManager
2. 设置pic02图片中四项中的任意一项或多项,设置内容如下:
数字(代表键盘上的数字键),
字母(代表键盘上的字母),
mouse 0(鼠标左键),
mouse 1(鼠标右键),
mouse 2(代表鼠标中键),除上面提到的几种以外
3. 注意Type项,选择Key or Mouse Button项

备注:其它的输入均会被认为是错误输入,unity软件会自动清空


个人比较习惯:如果使用键盘输入使用Input.GetKeyDown(KeyCode.K) 和如果使用鼠标输入使用Input.GetMouseButtonDown(0)​,原因是这两种比较直接,一眼就可以看出是键盘输入还是鼠标输入,不像input.getbuttondown("")还需要到InputManager中去设置

Input.getButtonDown兼有Input.GetKeyDown(0)和Input.getMouseButtonDown(0)两种功能,就是使用的时候有点绕,需要到InputManger设置​​

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { private Rigidbody2D rb; private BoxCollider2D coll; private SpriteRenderer sprite; private Animator anim; [SerializeField] private LayerMask jumpableGround; private float dirX = 0f; [SerializeField] private float moveSpeed = 7f; [SerializeField] private float jumpForce = 7f; private enum MovementState { idle, running, jumping, falling } // Start is called before the first frame update private void Start() { rb = GetComponent<Rigidbody2D>(); coll = GetComponent<BoxCollider2D>(); sprite = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); } } // Update is called once per frame private void Update() { dirX = Input.GetAxisRaw("Horizontal"); rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y); if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); } UpdateAnimationState(); } private void UpdateAnimationState() { MovementState state; if (dirX > 0f) { state = MovementState.running; sprite.flipX = false; } else if (dirX < 0f) { state = MovementState.running; sprite.flipX = true; } else { state = MovementState.idle; } if (rb.velocity.y > .1f) { state = MovementState.jumping; } else if (rb.velocity.y < -.1f) { state = MovementState.falling; } anim.SetInteger("state", (int)state); } private bool IsGrounded() { return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround); } }
最新发布
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值