Jickery
Jickery

Reputation: 247

Unity: GameObject always at center regardless of position changes

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

Upvotes: 2

Views: 2290

Answers (2)

Allen
Allen

Reputation: 567

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

Upvotes: 0

Fattie
Fattie

Reputation: 12336

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.

Upvotes: 1

Related Questions