【问题标题】:Unity: GameObject always at center regardless of position changesUnity:无论位置变化如何,游戏对象始终位于中心
【发布时间】:2017-05-30 03:29:55
【问题描述】:

我正在开发一个 2D 游戏,并使用 C# 脚本创建了一个游戏对象,如下所示。我还将我的相机设置为正交,并根据屏幕的宽度调整了我的精灵。无论我设置的位置如何,对象始终位于屏幕的中心。我该如何解决这个问题?

using UnityEngine;
using System.Collections;

public class TestingPositions : MonoBehaviour {

 GameObject hero;
 Sprite heroSprite;
 Vector3 heroPosition;
 // Use this for initialization
 void Start () {

       hero = new GameObject ();
       Instantiate (hero, heroPosition, Quaternion.identity);
     Camera camera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
     heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/4, Screen.height/4, camera.nearClipPlane));
     heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
     SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>();        renderer.sprite = heroSprite;
 }
}

【问题讨论】:

  • Instantiate 中使用后设置heroPosition,并将其设置为Screen.width/2Screen.height/2。那个代码很乱。另外,你为什么不为你的角色创建一个预制件,而不是从头开始创建整个游戏对象?
  • 如何使用预制件为我的游戏对象实现上述位置?

标签: unity3d unity5


【解决方案1】:

当你使用实例化时,你必须使用它

现有模型。

实例化的意思是“复制这个模型”或“复制这个模型”,或者“制作一个新的,以这个模型为例”。

您正在做的是创建一个全新的空“英雄”游戏对象 - 然后“实例化”它。这是没有意义的,什么都不做。

每当你想使用“实例化”时,你必须做的是:

 public GameObject modelPerson;

请注意,名称必须为“modelSomething”。

首先将其放入您的代码中。看督察。制作你真正的模特英雄(或任何它)

把它放在镜头外看不见的地方。

现在,将该东西拖到到 Inspector 中的“modelPerson”槽中。

如果您不熟悉在 Unity 中使用 Inspector-dragging 的基础知识,请查看基本 Unity 教程https://unity3d.com/learn/tutorials/topics/scripting

接下来在你的代码中,也许在 Start 中,试试这个

GameObject newHero = Instantiate( modelPerson );
newHero.transform.position = .. whatever you want
newHero.transform.rotation = .. whatever you want
newHero.name = "Dynamically created";
newHero.transform.parent = .. whatever you want

一旦您了解了这些基础知识,就可以进一步了解 Instantiate。你可以在不同的问题中提出这个问题。祝你好运。

【讨论】:

  • 比我快 16 秒 :P
【解决方案2】:
  1. 您需要保存对使用 Instantiate 创建的游戏对象的引用,because Instantiate makes a copy not modifies the original
  2. 要在实例化后修改游戏对象的位置,您需要使用 gameobject.transform.position = newPosition;要在实例化之前对其进行修改,您需要在实例化中使用 heroPosition 之前执行“heroPosition”行。

像这样:

using UnityEngine;
using System.Collections;

public class TestingPositions : MonoBehaviour
{

GameObject hero;
SpriteRenderer heroSprite;
// Use this for initialization
void Start()
{
    Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();

    //Save the reference to the instantiated object into a variable
    //Since you are creating an object from scratch, you don't even need Instantiate, which means copy - not create.
    hero = new GameObject();
    //Set its position
    hero.transform.position = camera.ScreenToWorldPoint(new Vector3(Screen.width / 4, Screen.height / 4, camera.nearClipPlane));
    //Set its rotation
    hero.transform.rotation = Quaternion.identity;
    //Add sprite renderer, save the reference
    heroSprite = hero.AddComponent<SpriteRenderer>();
    //Assign the sprite
    heroSprite.sprite = Resources.Load<Sprite>("Sprites/heroImage");
 }
}

【讨论】:

  • 你知道 OP 实际上需要了解关于实例化模型的知识......在这个阶段他不可能需要从头开始“构建”一个游戏对象......无论如何......
  • 他在 2D 中工作,他已经拥有的基本上是他“构建”游戏对象所需的一切,只需要重新排列;)
猜你喜欢
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多