C# 点球

C# 点球,c#,unity3d,C#,Unity3d,我正在做一个滚球游戏,球在移动,当球与立方体碰撞时,它会计数。我需要让比赛在每次球移动时都计数,但我似乎无法计算出来。我总是出错。谢谢你的帮助。 使用UnityEngine // Include the namespace required to use Unity UI using UnityEngine.UI; public class PlayerController : MonoBehaviour { // Create public variables for player

我正在做一个滚球游戏,球在移动,当球与立方体碰撞时,它会计数。我需要让比赛在每次球移动时都计数,但我似乎无法计算出来。我总是出错。谢谢你的帮助。 使用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();
    }
}
//包括使用Unity UI所需的命名空间
使用UnityEngine.UI;
公共类玩家控制器:单行为
{
//为玩家速度和文本UI游戏对象创建公共变量
公众浮标速度;
公共文本;
公共文本;
公共文本移动文本;
//创建对播放器上刚体组件的私有引用,以及到目前为止拾取的拾取对象的计数
私人刚体;
私人整数计数;
私人内部移动;
私人浮动位置;
//在比赛开始时。。
void Start()
{
//将刚体组件指定给我们的私有rb变量
rb=GetComponent();
//将计数设为零
计数=0;
//设置移动到零
移动=0;
//运行SetCountText函数以更新UI(见下文)
SetCountText();
//将Win文本UI的文本属性设置为空字符串,使“You Win”(游戏结束消息)为空
winText.text=“”;
位置=变换位置;
SetMovesText();
}
void LateUpdate()
{
if(位置!=transform.location)
{
移动=移动+1;
位置=变换位置;
SetMovesText();
}
}
//每个物理步骤。。
void FixedUpdate()
{
//将一些局部浮点变量设置为水平和垂直输入的值
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
//创建一个Vector3变量,并将X和Z指定给上面的水平和垂直浮动变量
Vector3移动=新Vector3(水平移动,0.0f,垂直移动);
//使用上面的“运动”矢量3为玩家刚体添加物理力,
//将其乘以“速度”——我们的公共玩家速度,显示在inspector中
rb.AddForce(移动*速度);
}
//当此游戏对象与碰撞器相交并选中“是否触发”时,
//将对撞机的引用存储在名为“other”的变量中。。
无效对撞机(对撞机其他)
{
//..如果我们相交的游戏对象具有指定的“拾取”标记。。
if(other.gameObject.CompareTag(“拾取”))
{
//使其他游戏对象(拾取)处于非活动状态,使其消失
other.gameObject.SetActive(false);
//将一个添加到分数变量“count”
计数=计数+1;
//运行“SetCountText()”函数(见下文)
SetCountText();
}
}
//创建一个独立的功能,可以更新“countText”用户界面,并检查是否已达到赢取所需金额
void SetCountText()
{
//更新“countText”变量的文本字段
countText.text=“Count:+Count.ToString();
//检查我们的“计数”是否等于或超过12
如果(计数>=12)
{
//设置“winText”的文本值
winText.text=“你赢了!”;
}
}
void SetMovesText()
{
movesText.text=“Moves:+Moves.ToString();
}
}

在统一中没有变换。位置。我相信你想知道物体的位置。这是通过
transform.position
完成的,对象的旋转可以通过
transform.rotation
修改

我需要让比赛在每次球移动时都计数

您可以通过检查何时变换位置来执行此操作

以下是根据我上面所说的需要进行的更改:

1。更改您的
专用浮点位置
私有向量3位置

2.Change
location=transform.location
位置=变换位置

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

4。最后更改
location=transform.location
位置=变换位置

固定代码:

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();
    }
}
公共类PlayerController:MonoBehavior
{
//为玩家速度和文本UI游戏对象创建公共变量
公众浮标速度;
公共文本;
公共文本;
公共文本移动文本;
//创建对播放器上刚体组件的私有引用,以及到目前为止拾取的拾取对象的计数
私人刚体;
私人整数计数;
私人内部移动;
私有向量3位置;
//在比赛开始时。。
void Start()
{
//将刚体组件指定给我们的私有rb变量
rb=GetComponent();
//将计数设为零
计数=0;
//设置移动到零
移动=0;
//运行SetCountText函数以更新UI(见下文)
SetCountText();
//将Win文本UI的文本属性设置为空字符串,使“You Win”(游戏结束消息)为空
winText.text=“”;
位置=变换位置;
SetMovesText();
}
void LateUpdate()
{
if(位置!=变换位置)
{
移动=移动+1;
位置=变换位置;
SetMovesText();
}
}
//每个物理步骤。。
void FixedUpdate()
{
//将一些局部浮点变量设置为水平和垂直输入的值
float moveHorizontal=Input.GetAxis(“水平”);