2014-02-18 43 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允許您檢測哪個設備正在使用,並允許非常簡單高效的多玩家支持。

針對不同遊戲手柄和平臺的映射可讓您爲標準佈局進行開發,您將花費更少的時間嘗試讓控件在不同平臺上或多或少地工作。