为什么我的Unity 2D平台游戏的安卓按钮控制器无法正常工作?



我是一名最近使用unity的游戏开发者,并且几乎放弃了处理这些内容。

我目前正在制作一款2d平台游戏。还有一个遗留下来的,android控制按钮。

我使用"标准资产",统一的"CrossPlatformInput"创建我需要的android控制器。

经过测试,在游戏中移动角色根本不起作用。我已经仔细地按照指南做了很多次,但结果还是一样

谁能告诉我发生了什么事?这是我正在使用的失败代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Pemain : MonoBehaviour
{
public Rigidbody2D rb;
public float movespeed = 5f;
public int jumppower;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
private Animator anim;
private int facing; 
void Start ()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
jumppower = 5;
facing = 1;
}
void FixedUpdate ()
{
onGround = Physics2D.OverlapCircle (groundCheck.position,groundCheckRadius,whatIsGround);
}
void Update () {
float move = CrossPlatformInputManager.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = new Vector2 (-movespeed, rb.velocity.y);
anim.SetBool ("Walking", true);
if (facing == 1)
{
transform.localScale = new Vector3 (-1f, 1f, 1f);
facing = 0;
}
} else if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
anim.SetBool ("Walking", true);
if (facing == 0)
{
transform.localScale = new Vector3 (1f, 1f, 1f);
facing = 1;
}
} else
{
anim.SetBool ("Walking", false);
}
if (Input.GetKey(KeyCode.Space) && onGround)
{
rb.velocity = new Vector2 (rb.velocity.x,jumppower);
}
}
}

您的代码中根本没有使用move变量。所以crossplatformminput不会对角色的移动产生任何影响。您应该将代码更改为类似于以下的内容:

float move = CrossPlatformInputManager.GetAxis("Horizontal");
if (move < -0.01f)
{
rb.velocity = new Vector2 (-movespeed, rb.velocity.y);
anim.SetBool ("Walking", true);
if (facing == 1)
{
transform.localScale = new Vector3 (-1f, 1f, 1f);
facing = 0;
}
} else if (move > 0.01f)
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
anim.SetBool ("Walking", true);
if (facing == 0)
{
transform.localScale = new Vector3 (1f, 1f, 1f);
facing = 1;
}
}

你可能还想刷一下你的c#技能,学习Unity同时编程是非常困难的。

最新更新