为什么我在Unity 2D中获得两个宽度和两个高度的精灵

时间:2015-02-26 09:20:14

标签: c# unity3d sprite

嗨,我在这里有点困惑,我想得到我的"播放器" GameObject和我能够获得宽度,但是当我调试.Log(宽度)时,我得到两个值12.8然后1.33为什么会这样?然后我用高度尝试了这个,我遇到了同样的问题,高度9.6和1.62有两个值我真的不知道这是什么。


好的,这是我的代码(我无法对其进行评论,评论时间太长)



using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour 
{
	public GameObject obstacle;
	public int score = 0;
	public float width;

	void Start () 
	{
		var renderer = GameObject.Find ("Crate").gameObject.GetComponent<SpriteRenderer>();
		width = renderer.bounds.size.x;
		Debug.Log (width);
	}

	void Update () 
	{
		if (obstacle != null && obstacle.transform != null) 
		{
			if(GameObject.Find("Player").transform.position.x >= obstacle.transform.position.x
			                                 && 
			   GameObject.Find("Player").transform.position.x < obstacle.transform.position.x + (width - 0.9) )
			{
				score++;
				//Debug.Log(score);
			}
		}
	}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

好了,您的代码已被重写。

请发布您的控制台日志的屏幕截图,包括前两个高度和宽度条目。

public class Score : MonoBehaviour
{
SpriteRenderer renderer;
Transform player;

public GameObject obstacle;
public int score = 0;
public float width;
public float height;

void Start()
{
    player = (Transform)GameObject.Find("Player").GetComponent<Transform>();
    renderer = GameObject.Find("Crate").gameObject.GetComponent<SpriteRenderer>();

    width = renderer.bounds.size.x;
    height = renderer.bounds.size.y;

    Debug.Log(width);
    Debug.Log(height);
}

void Update()
{
    if (obstacle != null && obstacle.transform != null)
    {
        if (player.position.x >= obstacle.transform.position.x &&
           player.position.x < obstacle.transform.position.x + (width - 0.9))
        {
            score++;
            //Debug.Log(score);
        }
    }
}
}