2015-07-21 192 views
-2

我目前正在一個遊戲中,用戶點擊屏幕(敵人),然後子彈應該從相機移動到觸摸位置殺死敵人。距離相機(槍)10英里處的敵人。C#拍攝移動觸摸

假設用戶在(x,y)= 5,5處觸摸,那麼子彈應該從攝像機位置0,0,-10行進到5,5,0。

我創建了一個spawnPoint並附加到相機,並且還將shooter腳本附加到spawnPoint。剛體射彈是子彈預製件。

請幫我解決這個問題。

+1

我不清楚你的問題到底是什麼。你問我們爲你提供物理代碼嗎?還是有些事情不像預期的那樣工作? – paulpdaniels

+0

你需要更具體地說明你的疑問,因爲你所描述的很難理解你迷失在哪裏。 – Frohlich

+0

如何實現這是我的問題 –

回答

0
float distance = 10f; 

void Update() 
{ 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

    Shoot(ray.GetPoint(distance)); 
} 

public void Shoot(float point) 
{ 
    // shoot from camera location to point here 
} 
0

下面的方法完美適用於基於觸摸的輸入。

Vector3 fingerPos;

void Update() 
{ 
    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 

    { 
     fingerPos = Input.GetTouch(0).position;   
     Vector3 pos = fingerPos; 
     pos.z = 5; 
     Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(pos); 
     transform.position = realWorldPos; 
    } 
} 
+0

我正在使用上面的代碼獲得觸摸輸入的真實世界位置。但是,當用戶觸摸屏幕時,如何從相機位置(0,0,-10)向實際位置(x,x,5)拍攝子彈。 –