【发布时间】:2017-12-29 04:00:03
【问题描述】:
我开始练习 Unity 和 c# 已经 4-5 天了。我很业余。我有一个我无法弄清楚的问题。我正在尝试编写类似 2D 乒乓球的简单游戏。每次球员错过球时,我都会实例化球并计算得分。但是即使每次都实例化球,游戏也会在1次后停止增加分数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ballmove : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public GameObject top;
private Vector2 direct;
public int p2;
public int p1;
public Text scoretext;
public Text scoretext2;
void Start()
{
rb = GetComponent<Rigidbody2D>();
speed = 3f;
InvokeRepeating("speedUp", 10f, 10f);
p1 = 0;
p2 = 0;
}
void Update()
{
if (rb.velocity == new Vector2(0f, 0f))
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(2f, 1f) * speed;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "finish2")
{
Debug.Log("This if is working");
p2++;
Instantiate(top, new Vector2(-6.4f, -0.2f), Quaternion.identity);
Destroy(this.gameObject);
updateScore2();
}
if (other.gameObject.tag == "finish")
{
p1++;
Instantiate(top, new Vector2(6.32f, -0.2f), Quaternion.identity);
Destroy(this.gameObject);
updateScore();
}
}
void updateScore()
{
scoretext.text = p1.ToString();
}
void updateScore2()
{
scoretext2.text = p2.ToString();
}
void speedUp()
{
direct = rb.velocity.normalized;
rb.velocity += direct * 2f;
}
}
我的 p1 和 p2(分数)都增加了 1 倍。但它们与
处于相同的条件下实例化并且每次其中一个玩家得分时,实例化工作但 p1 和/或 p2 停留在
值 1。
谢谢。
【问题讨论】:
标签: c# unity3d counter counting