2017-04-13 44 views
0

我有一個腳本來動我的角色(玩家) 腳本應該是罰款,它沒有任何錯誤,但是當我按下播放鍵我嘗試使用箭頭,它不工作,我不知道爲什麼。團結球員的跑動不工作

這是代碼。我感謝所有幫助你可以給我,謝謝

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class PlayerMovement : MonoBehaviour 
{ 

Direction currentDir; 
Vector2 input; 
bool isMoving = false; 
Vector3 startPos; 
Vector3 endPos; 
float t; 

public float walkSpeed = 3f; 

// Update is called once per frame 
void Update() 
{ 

    if (isMoving) 
    { 
     input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); 
     if (Mathf.Abs(input.x) > input.y) 
      input.y = 0; 
     else 
      input.x = 0; 

     if (input != Vector2.zero) 
     { 
      StartCoroutine(Move(transform)); 
     } 
    } 

} 

public IEnumerator Move(Transform entity) 
{ 

    isMoving = true; 
    startPos = entity.position; 
    t = 0; 

    endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + 
     System.Math.Sign(input.y), startPos.z); 

    while (t < 1f) 
    { 
     t += Time.deltaTime * walkSpeed; 
     entity.position = Vector3.Lerp(startPos, endPos, t); 
     yield return null; 
    } 

    isMoving = false; 
    yield return 0; 

} 


enum Direction 
{ 
    North, 
    East, 
    South, 
    West 
} 
    } 
+1

這是錯的。在更新功能中重複啓動「移動」協同程序.....您正在嘗試像在FPS遊戲中一樣移動? – Programmer

+0

我試圖讓它像2D遊戲一樣移動。爲了爭吵讓我們說像口袋妖怪:) –

+0

好吧,我明白了。如果是這樣的話,這可能是好的。你如何確定停止移動/目的地位置?我想你應該解釋玩家何時開始移動?也許當鼠標點擊? – Programmer

回答

2

變化

void Update() 
{ 
    if (isMoving) 
    { 

void Update() 
{ 
    if (!isMoving) 
    { 

否則,每個更新您檢查isMoving變量做什麼,如果是假的。其中isMoving可能成爲真正的唯一的地方就是你的移動協同程序,但它只能從更新,它沒有做任何事情,因爲isMoving是假的推出。

+0

由於這實際上是有道理的。只是我遇到的一個問題:如果我按下箭頭移動它,它就會消失o對此有何看法? –