2016-11-16 71 views
0

我有一個統一遊戲,並且在其中有一個旋轉的遊戲對象,它在點擊時會提高其速度。停止旋轉,在遊戲對象上單擊右鍵

我的問題是,遊戲無法正常工作,因爲我想要它。現在,如果我點擊屏幕的任何部分,它會增加遊戲對象的旋轉速度。另一方面,如果我將手指放在屏幕上,遊戲對象開始減速,然後開始向相反的方向旋轉。

我希望旋轉的對象增加,當我點擊它,而不僅僅是如果我點擊屏幕的任何部分。此外,我不知道爲什麼壓低反轉的方向。

var speed = 1; 
var click = 0; 

Screen.orientation = ScreenOrientation.LandscapeLeft; 

function Update(){ 

{ 
    transform.Rotate(0,0,speed); 
} 

if(Input.GetMouseButton(0)) 
{ 

    if(speed != 0) 
    { 
     speed = 0; 
    } else { 
     click++; 
     speed = click; 
    } 

回答

0

必須使用輸入。 GetMouseButtonUp or Input.GetMouseButtonDown,NOT A Input.GetMouseButton,此方法用於夾緊。

嘗試使用此代碼:

var speed = 1; 
var click = 0; 

Screen.orientation = ScreenOrientation.LandscapeLeft; 

function Update(){ 

{ 
    transform.Rotate(0,0,speed); 
} 

if(Input.GetMouseButtonDown(0)) 
{ 

    if(speed != 0) 
    { 
     speed = 0; 
    } else { 
     click++; 
     speed = click; 
    } 
0

的幾個問題在這裏:

首先:

要提高速度在單擊僅在對象上,使用由攝像頭在光線投射,並檢查它是否擊中你的對象。你的物體需要在它上面有一個對撞機部件才能工作。

RaycastHit hit; 
Ray ray = camera.ScreenPointToRay(Input.mousePosition); 

if (Physics.Raycast(ray, out hit)) 
{ 
    Transform objectHit = hit.transform; 
    objectHit.Rotate(0,0,speed); 
} 

參見https://docs.unity3d.com/Manual/CameraRays.html - >光線投射部,以獲取更多信息。

其次:

Input.GetMouseButtonDown(..) // returns true at the instance your mouse button transits from up to down 
Input.GetMouseButton(..) // returns true as long as your mouse button is held down 

使用Input.GetMouseButtonDown(..)Update()方法,如果你想要做的,當你點擊它的東西。