2017-10-16 35 views
0

我正在做一個滾球遊戲,球移動並計數球與立方體碰撞時的數量。每當球移動時,我都需要計數遊戲,但我似乎無法弄清楚。我不斷收到錯誤。我感謝任何幫助。 使用UnityEngine;球數滾球

// Include the namespace required to use Unity UI 
using UnityEngine.UI; 

public class PlayerController : MonoBehaviour 
{ 

    // Create public variables for player speed, and for the Text UI game objects 
    public float speed; 
    public Text countText; 
    public Text winText; 
    public Text movesText; 

    // Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far 
    private Rigidbody rb; 
    private int count; 
    private int moves; 
    private float location; 

    // At the start of the game.. 
    void Start() 
    { 
     // Assign the Rigidbody component to our private rb variable 
     rb = GetComponent<Rigidbody>(); 

     // Set the count to zero 
     count = 0; 

     // Set moves to zero 
     moves = 0; 

     // Run the SetCountText function to update the UI (see below) 
     SetCountText(); 

     // Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank 
     winText.text = ""; 

     location = transform.location; 

     SetMovesText(); 


    } 

    void LateUpdate() 
    { 
     if (location != transform.location) 
     { 
      moves = moves + 1; 
      location = transform.location; 
      SetMovesText(); 
     } 



    } 

    // Each physics step.. 
    void FixedUpdate() 
    { 
     // Set some local float variables equal to the value of our Horizontal and Vertical Inputs 
     float moveHorizontal = Input.GetAxis("Horizontal"); 
     float moveVertical = Input.GetAxis("Vertical"); 

     // Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above 
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); 

     // Add a physical force to our Player rigidbody using our 'movement' Vector3 above, 
     // multiplying it by 'speed' - our public player speed that appears in the inspector 
     rb.AddForce(movement * speed); 
    } 

    // When this game object intersects a collider with 'is trigger' checked, 
    // store a reference to that collider in a variable named 'other'.. 
    void OnTriggerEnter(Collider other) 
    { 
     // ..and if the game object we intersect has the tag 'Pick Up' assigned to it.. 
     if (other.gameObject.CompareTag("Pick Up")) 
     { 
      // Make the other game object (the pick up) inactive, to make it disappear 
      other.gameObject.SetActive(false); 

      // Add one to the score variable 'count' 
      count = count + 1; 

      // Run the 'SetCountText()' function (see below) 
      SetCountText(); 

     } 

    } 

    // Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved 
    void SetCountText() 
    { 
     // Update the text field of our 'countText' variable 
     countText.text = "Count: " + count.ToString(); 

     // Check if our 'count' is equal to or exceeded 12 
     if (count >= 12) 
     { 
      // Set the text value of our 'winText' 
      winText.text = "You Win!"; 
     } 

    } 

    void SetMovesText() 
    { 
     movesText.text = "Moves: " + moves.ToString(); 
    } 
} 
+0

請重新使用語法高亮。 –

回答

5

Unity中沒有這樣的東西,如transform.location。我相信你想獲得對象的位置。這是通過transform.position完成的,物體的旋轉可以用transform.rotation進行修改。

我需要爲遊戲通過檢查時transform.position更改每個球移動

你可以做到這一點的時間計數。

下面是需要在上面我所說的基於要做出的改變:

.Change您private float location;private Vector3 location;

.Change location = transform.location;location = transform.position;

。更改if (location != transform.location)if (location != transform.position)

。最後改變location = transform.location;location = transform.position;

固定代碼:

public class PlayerController : MonoBehaviour 
{ 

    // Create public variables for player speed, and for the Text UI game objects 
    public float speed; 
    public Text countText; 
    public Text winText; 
    public Text movesText; 

    // Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far 
    private Rigidbody rb; 
    private int count; 
    private int moves; 
    private Vector3 location; 

    // At the start of the game.. 
    void Start() 
    { 
     // Assign the Rigidbody component to our private rb variable 
     rb = GetComponent<Rigidbody>(); 

     // Set the count to zero 
     count = 0; 

     // Set moves to zero 
     moves = 0; 

     // Run the SetCountText function to update the UI (see below) 
     SetCountText(); 

     // Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank 
     winText.text = ""; 
     location = transform.position; 
     SetMovesText(); 
    } 

    void LateUpdate() 
    { 
     if (location != transform.position) 
     { 
      moves = moves + 1; 
      location = transform.position; 
      SetMovesText(); 
     } 
    } 

    // Each physics step.. 
    void FixedUpdate() 
    { 
     // Set some local float variables equal to the value of our Horizontal and Vertical Inputs 
     float moveHorizontal = Input.GetAxis("Horizontal"); 
     float moveVertical = Input.GetAxis("Vertical"); 

     // Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above 
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); 

     // Add a physical force to our Player rigidbody using our 'movement' Vector3 above, 
     // multiplying it by 'speed' - our public player speed that appears in the inspector 
     rb.AddForce(movement * speed); 
    } 

    // When this game object intersects a collider with 'is trigger' checked, 
    // store a reference to that collider in a variable named 'other'.. 
    void OnTriggerEnter(Collider other) 
    { 
     // ..and if the game object we intersect has the tag 'Pick Up' assigned to it.. 
     if (other.gameObject.CompareTag("Pick Up")) 
     { 
      // Make the other game object (the pick up) inactive, to make it disappear 
      other.gameObject.SetActive(false); 

      // Add one to the score variable 'count' 
      count = count + 1; 

      // Run the 'SetCountText()' function (see below) 
      SetCountText(); 
     } 
    } 

    // Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved 
    void SetCountText() 
    { 
     // Update the text field of our 'countText' variable 
     countText.text = "Count: " + count.ToString(); 

     // Check if our 'count' is equal to or exceeded 12 
     if (count >= 12) 
     { 
      // Set the text value of our 'winText' 
      winText.text = "You Win!"; 
     } 

    } 

    void SetMovesText() 
    { 
     movesText.text = "Moves: " + moves.ToString(); 
    } 
} 
+0

謝謝。你真的救了我很多壓力。 –

+1

@Brandi如果這個答案幫了你,接受它作爲答案。它可以幫助那些可能有相同問題的人更快地找到答案,對於花時間回覆的人來說這是一個很好的「謝謝」。 –