Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


spawn GameObject at certain position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 welly_59   Members   -  Reputation: 230

Like
0Likes
Like

Posted 01 April 2014 - 04:21 AM

im trying to spawn a ball when the player collects a powerup in my breakout clone. But the problem i am having is that the gameobject spawns in the same place all the time.

public void SpawnBallClone()
    {
        Vector3 pos = GameObject.FindWithTag("Paddle").GetComponent<PaddleManager>().PaddlePosition();
        print(pos + "from the SpawnBallClone Method");
        Instantiate(Ball, pos, Quaternion.identity);
    }

the Vector3 pos is the correct value, i am using the print() to make sure the values are correct:

 

 

(5.7, -4.1, 0.0)from the SpawnBallClone Method

 

But when the Ball GameObject is instantiated it always instantiates at 0,0,0.

 

What am i missing here?



Sponsor:

#2 welly_59   Members   -  Reputation: 230

Like
0Likes
Like

Posted 01 April 2014 - 04:27 AM

Looking at my initial ball spawn it seems i can also not affect the positioning of that either. On game start i am doing this:

 

public void SpawnBall()
    {

        StartCoroutine(WaitForBallCoroutine());
         
       
    }

    IEnumerator WaitForBallCoroutine(){
        Debug.Log ("about to yield return WaitForSeconds(1)");
        yield return new WaitForSeconds(1);


        Instantiate(Ball, new Vector3(0, 0, 0), Quaternion.identity);


    }
 
and whatever values i put into the ball instantiate it will always spawn at 0,0,0.
 
I am using the same code to instantiate the blocks at the beggining of a level and that works....so any tips on what is wrong/different would be great
 
    void CreateBlocks()
    {
        float XAxis = -6f;
        float YAxis = 4.5f;

        for (int rows = 0; rows < 5; rows++)
        {
            for (int columns = 0; columns < 13; columns++)
            {
                Instantiate(Block, new Vector3(XAxis, YAxis, 0), Quaternion.identity);
                XAxis = XAxis + 1f;
            }
            XAxis = -6f;
            YAxis = YAxis - 0.5f;
            Instantiate(Block, new Vector3(XAxis, YAxis, 0), Quaternion.identity);
        }

    }

Edited by welly_59, 01 April 2014 - 04:27 AM.


#3 welly_59   Members   -  Reputation: 230

Like
4Likes
Like

Posted 01 April 2014 - 01:18 PM

sorted it, my start() contained a vector3 which was positioning it at 000. i removed that and created a call to the spawnmethod on creating the scene sending it a vector3 instead



#4 NotYourAverageUser   Members   -  Reputation: 500

Like
1Likes
Like

Posted 01 April 2014 - 02:00 PM

sorted it, my start() contained a vector3 which was positioning it at 000. i removed that and created a call to the spawnmethod on creating the scene sending it a vector3 instead

Glad you figured it out.  I didn't see anything in the code above that would have caused your issue.  It is quite often that the offending code is often left out of posts where unexpected results are occurring.

 

Nonetheless, it is always a good idea to return to your posts with the solution, even if found by yourself.  I commend you for that.



#5 welly_59   Members   -  Reputation: 230

Like
0Likes
Like

Posted 01 April 2014 - 02:40 PM

i had a script attached to the ball called ballcontroller. when i first started i wanted a quick way to get the ball in the middle of the screen so my ballcontroller.start() contained this line:

 

transform.position = new Vector2 (0, 0);

 

When i was instantiated a new ball the start() was moving it back to 0,0,0. What i have done is removed that and instantiated the ball using my gamecontroller script which is attached to main camera, creating a spawnball() in ballcontroller to handle new balls.

 

ive made this so difficult by not planning it all beforehand. Adding new features sometimes means i have to redo a lot of code . note to self for next time i suppose!



#6 NotYourAverageUser   Members   -  Reputation: 500

Like
0Likes
Like

Posted 02 April 2014 - 06:37 AM


using my gamecontroller script which is attached to main camera

 

It would be more idiomatic to not have the game controller script attached to your main camera.  Why should your camera be responsible for controlling your game? 

 

Might as well create a game object, call it - Game Manager/Controller - attach the game controller script to that object.  This object can hold all general components (ambient sounds, ball spawn locations, score/hiscore, things that are general for the game and not attached to a specific object. 

 

Your camera should be for presenting the view.  Just a little cleaner in my opinion.



#7 welly_59   Members   -  Reputation: 230

Like
0Likes
Like

Posted 03 April 2014 - 03:58 PM

thanks for that advice. i didnt think about creating an empty object!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS