【问题标题】:Using Getaxis to move an object with both mouse and arrows keys along the x axis in c#/Unity3D?在 c#/Unity3D 中使用 Getaxis 沿 x 轴使用鼠标和箭头键移动对象?
【发布时间】:2016-06-15 08:46:06
【问题描述】:

背景:我正在制作一个突破性克隆。我有一个桨的脚本,它可以通过鼠标移动成功地沿 X 轴移动桨。

愿望:我想为这个游戏有两个输入。一个是鼠标,另一个输入是左右方向键。我进入了编辑---->项目设置---->输入,但我不确定如何调整 Alt Negative、Alt positive 按钮以与 x 轴上的鼠标对齐。

我还想根据我的游戏屏幕大小限制移动。我知道我必须使用 Mathfclamp 方法,但是我不确定如何在我的脚本中以正确的顺序进行格式化。

这是我用于 GetAxis 的代码。

public class Paddle : MonoBehaviour {


   public float speed;


    void Update () 
    {
        float x = Input.GetAxis("Mouse X")* Time.deltaTime*speed;


        transform.Translate (x,0,0);

    }
}

【问题讨论】:

  • 你让它工作了吗?

标签: c# unity3d


【解决方案1】:

您不能将Input.GetAxis 与箭头键一起使用,但有一个技巧可以做到这一点。首先,使用Input.GetAxis获取鼠标移动值并将其存储到变量x中。如果x 的值moreless 小于0,则移动带有来自Input.GetAxis 的值的对象。

如果x的值为0,检查是按左箭头还是右箭头,然后根据哪个键使x的方向为Vector3.right*speedVector3.left.x * keySpeed被按下。要知道的另一件事是,两个速度乘数必须是不同的值。鼠标应该有一个速度倍增器,键盘应该有另一个倍速器,否则对象将从屏幕上消失。鼠标的适当值为100f,键盘的适当值为0.1f

public class Paddle : MonoBehaviour
{
    public float mouseSpeed = 100;
    public float keySpeed = 0.1f;


    void Update()
    {
        float x = 0;
        x = Input.GetAxis("Mouse X") * Time.deltaTime * mouseSpeed;

        if (x == 0)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                x = Vector3.left.x * keySpeed;
            }

            if (Input.GetKey(KeyCode.RightArrow))
            {
                x = Vector3.right.x * keySpeed;
            }

        }
        transform.Translate(x, 0, 0);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多