Input.GetAxis for android

本文关键字:android for GetAxis Input | 更新日期: 2023-09-27 17:57:08

我正在创建一个迷宫游戏,目前我的安卓控制有延迟。我已经习惯了计算机控件向前移动,向左和向右转,并使用input.getaxis跳跃。现在在 android 上,我创建了可以工作的按钮,但不是我想要的。它可以随心所欲地跳跃和转动,但它不能正常移动。有两个问题,一个是当我单击时它会移动一点,然后我需要再次单击,但我想按住并且它会连续移动,第二个问题是当我转动字符时,它总是在 x 轴上移动输入.getaxis 一切正常,但我需要一些适合 Androdid 的东西,就像这样, 这是我的代码:

using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
    public float speed;
    public float speedj;
    Rigidbody rb;
    static Animator anim;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<Animator>();
    }
    public void Walk ()
    {
        anim.SetTrigger ("Walk");
        rb.velocity = new Vector3 (0,0,7f);
    }
    public void Left ()
    {
        transform.Rotate(0,-90f,0);
    }
    public void Right ()
    {
        transform.Rotate(0,90f,0);
    }
    public void Jump ()
    {
        anim.SetTrigger ("Jump");
        rb.velocity = new Vector3 (0,4f,0);
    }
    public void StopVelocity ()
    {
        rb.velocity = Vector3.zero;
        transform.Translate(0,0,0);
    }
    void FixedUpdate ()
    {
        float x = Input.GetAxis ("Horizontal") * Time.deltaTime * speed;
        float z = Input.GetAxis ("Vertical") * Time.deltaTime * speed;
        transform.Translate (0,0,z);
        transform.Rotate (0,x,0);
        if(Input.GetKey(KeyCode.Space))
        {
            anim.SetTrigger ("Jump");
            rb.velocity = new Vector3 (0,speedj,0);
        }
    }
}

所以这是固定的更新功能是计算机的,其他的用于安卓的按钮,我正在使用事件触发器来设置指针向上和向下的事件,请帮助我更改代码,以便它适用于安卓,就像它适用于计算机的 Input.getAxis 一样,谢谢!

Input.GetAxis for android

"当我点击时,它会移动

一点,然后我需要再次点击,但我想按住,它会连续移动。

在此处使用 EventTrigger 应该是最佳方法。查看 OnPointerDown();应为每一帧调用该函数。您应该将事件触发器附加到按钮上,以便它在手机上工作。

"当我转身时,角色不会以直立的方式移动。"

尝试使用本地变换向前移动。即 Transform.Forward()。它将允许您向前移动到您之前设置的方向,前进始终是规范化向量3。按照你的方式移动将使用世界轴,你的旋转根本不重要。