android - 如何使用更改以下代码在使用触摸控件的设备上工作?
问题描述
我正在使用 Unity 和 C# 创建游戏。下面的代码使玩家可以使用计算机上的箭头键左右移动,但我需要它在 Andriod 设备上工作。有没有一种简单的方法可以使用此代码通过使用触摸控件而不重写所有代码来使游戏在 Andriod 设备上运行?
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myRigidbody.velocity = new Vector3(moveSpeed,
myRigidbody.velocity.y, 0);
}
else if (Input.GetAxisRaw("Horizontal") < 0f)
{
myRigidbody.velocity = new Vector3(-moveSpeed,
myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y,0f);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
}
myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
myAnim.SetBool("Grounded", isGrounded);
}
}
解决方案
尝试这个。三个变量(leftBtn、rightBtn 和 jump)将被相应地触发:
- 如果触摸屏幕的右下角,rightBtn = true
- 如果触摸屏幕左下角,leftBtn = true
- 如果触摸屏幕上半部分的任意位置,则跳转 = true
-
bool leftBtn = false;
bool rightBtn = false;
bool jump = false;
void Update()
{
leftBtn = false;
rightBtn = false;
jump = false;
if(Input.touches.Length > 0){
Touch touch = Input.touches [0];
if (touch.position.y > Screen.height / 2) {
jump = true;
} else {
if (touch.position.x > Screen.width / 2) {
rightBtn = true;
} else {
leftBtn = true;
}
}
}
//put the rest Of your code here \/
}
因此,我认为这将是您的代码中的样子(但我可能走错路了):
bool leftBtn = false;
bool rightBtn = false;
bool jump = false;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
leftBtn = false;
rightBtn = false;
jump = false;
if(Input.touches.Length > 0){
Touch touch = Input.touches [0];
if (touch.position.y > Screen.height / 2) {
jump = true;
} else {
if (touch.position.x > Screen.width / 2) {
rightBtn = true;
} else {
leftBtn = true;
}
}
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (rightBtn)
{
myRigidbody.velocity = new Vector3(moveSpeed,
myRigidbody.velocity.y, 0);
}
else if (leftBtn)
{
myRigidbody.velocity = new Vector3(-moveSpeed,
myRigidbody.velocity.y, 0f);
}
else
{
myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y,0f);
}
if (jump && isGrounded)
{
myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
}
myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
myAnim.SetBool("Grounded", isGrounded);
}
如果您愿意,可以使用 Screen.width / 2 和 Screen.height / 2 来获得屏幕的不同区域触发不同的功能。
推荐阅读
- python - 获取“500 内部服务器错误”向 AWS lambda 端点发送 POST 请求
- python - 如何在熊猫中按一个共同值分组并根据它分成列?
- javascript - Chrome 无法正确识别背面。(HTML、CSS、背面可见性:隐藏)
- ruby-on-rails - 安装 ruby 3 时出现段错误 11
- vue.js - 如何为 Vue 3 中的代理数据赋值?
- css - @keyframes 无限循环
- python - 使用 GraphAPI 的 Facebook Messenger 对话的日期过滤器
- discord - 被 Discord 阻止的 Repli Discord 机器人
- postgresql - 如何从 postgres 的表中删除超过 90 天的记录
- php - 如何在php中从复活节日期减去1周