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

Why I am getting two widths and two heights of sprite in Unity 2D

嗨,我在这里有点困惑我想获得我的 "Player" GameObject 的宽度并且我能够获得宽度但是当我 Debug.Log(width) 我得到两个值 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);
   }
  }
 }
}

好的,这是您重写的代码。

请 post 控制台日志的屏幕截图,包括高度和宽度的前两项。

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);
        }
    }
}
}