2014-02-18 46 views
1

我目前正在尝试在Unity中创建2d平台游戏,除了一件事情之外,一切工作都正常。当我向左或向右推,我的精灵动画镜像到正确的方向。我目前正在尝试添加控制器输入,但我似乎无法左右推动模拟器。推杆时它应该是镜像,反之亦然。Unity 2d平台游戏控制器支持

希望有人能帮助我:)

#pragma strict 

var X : float; 

function Start() { 
//Gathering normal object scale 
X = transform.localScale.x; 
} 

function Update() { 
    if(Input.GetKey("a")) { 
    // Gamer pushes left arrow key 
    // Set texture to normal position 
    transform.localScale.x = -X; 
} 
else if (Input.GetKey("d")) { 
    // Gamer pushes right arrow key 
    // Flip texture 
    transform.localScale.x = X; 
} 
if(Input.GetKey("left")) { 
    // Gamer pushes left arrow key 
    // Set texture to normal position 
    transform.localScale.x = -X; 
} 
else if (Input.GetKey("right")) { 
    // Gamer pushes right arrow key 
    // Flip texture 
    transform.localScale.x = X; 
} 
if(Input.GetAxis("Horizontal")) { 
    // Gamer pushes left arrow key 
    // Set texture to normal position 
    transform.localScale.x = -X; 
} 
else if (Input.GetAxis("Horizontal")) { 
    // Gamer pushes right arrow key 
    // Flip texture 
    transform.localScale.x = X; 
    } 
} 

回答

1

你有这样的:

if(Input.GetAxis("Horizontal")) { 
// Gamer pushes left arrow key 
// Set texture to normal position 
transform.localScale.x = -X; 
} 
else if (Input.GetAxis("Horizontal")) { 
// Gamer pushes right arrow key 
// Flip texture 
transform.localScale.x = X; 
} 

第二否则,如果将总是被调用,因为你正在检查完全相同的Input.GetAxis() 。

尝试这样的事:

if (Input.GetAxis("Horizontal") < 0) 
{ 
    transform.localScale.x = -X; 
} 
else if (Input.GetAxis("Horizontal") > 0) 
{ 
    transform.localScale.x = X; 
} 

Input.GetAxis(“水平”)检查,可以完全按下左右键和吐出数量取决于左或右键...

如果我按“左箭头”键,它会返回一个NU介于0和-1之间。 如果我按'右箭头'键,它会返回一个介于0和1之间的数字。

这是否有意义?

+0

你好,是的,它确实没什么意义,我现在在课堂上,所以我没有机会用我的控制器测试脚本。所以这个脚本意味着如果水平<0向左移动,并且如果水平> 0向右移动?统一自动理解我的左边模拟是否是输入? 除此之外,我还在unity的输入部分添加了我的有线控制器的D-pad。它确实有效,但我必须添加2个水平轴,一个用于D-pad的左侧箭头,另一个用于右侧。 –

+0

@QuincyNorbert叶我不完全确定你如何设置输入,但有一个阅读下面的链接,看看是否有帮助... http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis .html 因此,您已经创建了自己的定制d-pad? – Savlon

+0

我试过你的方法,现在正在工作,非常感谢!我曾尝试过一次,但忘记在else if语句中添加> 0 –

0

我会完全抛弃标准的Unity输入,这是非常可怕的,并切换到InControl。

他们的API允许您检测哪个设备正在使用,并允许非常简单高效的多玩家支持。

针对不同游戏手柄和平台的映射可让您为标准布局进行开发,您将花费更少的时间尝试让控件在不同平台上或多或少地工作。