Unity: GameObject always at center regardless of position changes

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 13:23:51

问题


I am working on a 2D game and have created a game object using C# script as below. I also set my camera to orthogonal and have adjusted my sprite based on the width of the screen. Regardless of the position I set, the object is always at the center of the screen. How can I solve this?

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

回答1:


when you use Instantiate you have to use it on

an existing model.

Instantiate means "duplicate this model" or "copy this model", or "make a new one, using this model as an example".

What you are doing, is creating a brand new empty "hero" game object - and then "instantiating" it. That is meaningless and does nothing.

What you must do whenever you want to use "Instantiate" is this:

 public GameObject modelPerson;

Note that the name must be "modelSomething".

first put that in your code. LOOK at the Inspector. MAKE your actual model hero (or whatever it is)

Sit it somewhere off camera where it is not seen.

Now, drag that thing to the "modelPerson" slot in the Inspector.

If you are not familiar with the basics of using Inspector-dragging in Unity, review basic Unity tutorials https://unity3d.com/learn/tutorials/topics/scripting

Next in your code, perhaps in Start, try this

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

once you understand these basics, there is very much more to learn about Instantiate. You can ask that in separate questions. Good luck.




回答2:


  1. Your need to save the reference to your gameObject that is created with Instantiate, because Instantiate makes a copy not modifies the original.
  2. To modify a gameobjects position after instantiation, you need to use gameobject.transform.position = newPosition; To modify it before instantiation, you would need to do the "heroPosition" line before using heroPosition in Instantiate.

So like this:

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


来源:https://stackoverflow.com/questions/36476332/unity-gameobject-always-at-center-regardless-of-position-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!