我正在做一个简单的拖放统一游戏。当我第一次拖动物体时它不会给出分数,但是当我再次拖动它时,即使我在错误的地方掉落也会得分。我正在使用标签来匹配所需的对象
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class sh_score : MonoBehaviour {
static int score = 0;
public Text scoreText;
public GameObject ans_circle;
public GameObject tag_circle;
public GameObject tag_rectangle;
public GameObject ans_rectangle;
public GameObject ans_triangle;
public GameObject tag_triangle;
public GameObject ans_square;
public GameObject tag_square;
public GameObject ans_star;
public GameObject tag_star;
void Start()
{
score = score;
if( (ans_circle == null || tag_circle == null) || (ans_rectangle == null || tag_rectangle == null)|| (ans_square == null || tag_square == null) || (ans_triangle == null || tag_triangle == null)|| (ans_star == null || tag_star == null))
{
ans_circle = GameObject.FindGameObjectWithTag("ans_circle");
if (ans_circle != null)
{ Debug.Log("ans Find"); }
tag_circle = GameObject.FindGameObjectWithTag("circle");
if (tag_circle != null)
{
Debug.Log("circle");
}
checkTagPlace();
ans_rectangle = GameObject.FindGameObjectWithTag("ans_rectangle");
if (ans_rectangle != null)
{ Debug.Log("ans Find"); }
tag_rectangle = GameObject.FindGameObjectWithTag("rectangle");
if (tag_circle != null)
{
Debug.Log("rectangle");
}
ans_triangle = GameObject.FindGameObjectWithTag("ans_triangle");
if (ans_triangle != null)
{ Debug.Log("ans Find"); }
tag_triangle = GameObject.FindGameObjectWithTag("triangle");
if (tag_triangle != null)
{
Debug.Log("triangle");
}
ans_star = GameObject.FindGameObjectWithTag("ans_star");
if (ans_star != null)
{ Debug.Log("ans Find"); }
tag_star = GameObject.FindGameObjectWithTag("star");
if (tag_star != null)
{
Debug.Log("star");
}
ans_square = GameObject.FindGameObjectWithTag("ans_square");
if (ans_square != null)
{ Debug.Log("ans Find"); }
tag_square = GameObject.FindGameObjectWithTag("square");
if (tag_square != null)
{
Debug.Log("square");
}
}
}
void update()
{
scoreText.text = score.ToString();
}
public void IncrementScore()
{
score = score + 9;
score++;
scoreText.text = "Score: " + score;
}
public void checkTagPlace()
{
Debug.Log("check function run");
float circle_postion = tag_circle.transform.position.x;
float circle_Tag_positon = ans_circle.transform.position.x;
float triangle_position = tag_triangle.transform.position.x;
float triangle_Tag_positon = ans_triangle.transform.position.x;
float square_postion = tag_square.transform.position.x;
float square_Tag_positon = ans_square.transform.position.x;
float star_postion = tag_star.transform.position.x;
float star_Tag_positon = ans_star.transform.position.x;
float rectangle_position = tag_rectangle.transform.position.x;
float rectangle_Tag_positon = ans_rectangle.transform.position.x;
if ((ans_circle.transform.position.x == tag_circle.transform.position.x))
{
Debug.Log("found position");
IncrementScore();
}
else if ((ans_rectangle.transform.position.x == tag_rectangle.transform.position.x))
{
IncrementScore();
}
else if ((ans_square.transform.position.x == tag_square.transform.position.x))
{
IncrementScore();
}
else if (ans_triangle.transform.position.x == tag_triangle.transform.position.x)
{
IncrementScore();
}
else if (ans_star.transform.position.x == tag_star.transform.position.x)
{
IncrementScore();
}
}
}
答案 0 :(得分:3)
我还不能发表评论,但
checkTagPlace
,这可能是导致错误的原因。答案 1 :(得分:1)
我相信你的问题可能就在这里:
if(ans_rectangle.transform.position.x == tag_rectangle.transform.position.x)
当您检查这些位置(以及其他如果这样的陈述)是否相等时,您正在检查他们是否完全相等。除非你有一些控制器使你的形状的运动离散化,这几乎永远不会发生。
我相信你想要的是这样的:
float epsilon=.001;
if(Math.abs(ans_rectangle.transform.position.x - tag_rectangle.transform.position.x)<epsilon)
或者,您可以为所有形状提供碰撞器,并实际检查两种形状类型之间的碰撞,可能使用图层蒙版来防止比较不同类型的形状。
我知道这并不能解释您所看到的所有行为,但这可能解释了为什么它第一次没有增加您的分数。
由于您没有包含启用拖动的代码,因此我们无法知道这是否是问题。