Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nansy_g21 · Oct 01, 2015 at 12:06 PM · instantiate2d-platformerflipping

Change starting position of Instantiate object

Hello,

I am new to unity, so there might be some mistakes in my logic but all ideas are welcome.

I am developing a 2D game multiplayer game.

I have to say that the screen is scrollable and only a part of the scene is visible on the camera every time. In every game for the first player I want the sprites to move from left to right, and for the second from right to left. So I use this code to flip the whole world

  void Flip()
     {
         //Get the current state of world to a temp variable
         Vector3 theScale = transform.localScale;
         //Reverse the vector
         theScale.x *= -1;
         //Set the vector as the new state of world
         transform.localScale = theScale;
 
         //Basically reverse the whole world including the sprite
     }

I have the original sprite created and it starts and moves normally from left to right or from right to left according to the player's turn using this code

  startingCollider = redStartingCollider;   // collider on left side of screen, same for player 1 and 2 because of world fliping
 
 //To set a default position
  Vector3 pos = transform.position;
 pos.x = startingCollider.transform.position.x;
 transform.position = pos;
 
  if (PlayerScript.playerTurn == 2)
         {
             speed *= -1;
         }

Now I want the user to create new sprites when clicking on a button and I want sprites to begin from the same position every time. So I use this code when the button is pressed

   GameObject ob=Instantiate(unit);
   ob.transform.parent = parentObject.transform;
 
 // To set default position not a relative to an object
  Vector3 pos3 = ob.transform.position;
 
 if (PlayerScript.playerTurn == 1)
  {
        pos3.x = redStartingCollider.transform.position.x; //collider on left side of screen
 }
 else
  {
        pos3.x = blueStartingCollider.transform.position.x; //collider on right side of screen
 }
 pos3.y = 13;
  ob.transform.position = pos3;

If the player's turn is 1 and there is no flip in the world the orignal sprite and it's children begin to move correctly from left to right.

The problem is when player's turn is 2 and the world flips. The original sprite begins from right and moves to left but it's children continues to begin from left.

What am I doing wrong? Any suggestions? Thanks in advance and sorry for the long post!

PS. Colliders that are used as starting points are not necessarily visible on the screen every time. I just want sprites to start from this specific point every time one is created.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Oct 01, 2015 at 01:53 PM 0
Share

Try using transform.localPosition ins$$anonymous$$d of transform.position, since the ob GameObject is a child of parentObject.

avatar image nansy_g21 Cherno · Oct 01, 2015 at 02:22 PM 0
Share

I tried it, yet nothing changed. The copies still start from left. If they are copies shouldn't they inherit the world settings from their parent? Shouldn't they behave the same way?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nansy_g21 · Oct 01, 2015 at 03:52 PM

Ok I figured out a solution not probably not the best one but it works at least for the moment. So for anyone with this kind of problem, I gave up the idea of flipping the whole world round. I just flipped the sprite using this code for the original sprite

 if (PlayerScript.playerTurn == 1)
  {
          startingCollider = redStartingCollider;
 }
 else
 {
           startingCollider = blueStartingCollider;
           speed = -5;
           transform.Rotate(new Vector3(0, 180, 0));
 }

And this code for the children

 if(PlayerScript.playerTurn==1)
                 {
                     startingCollider = redStartingCollider;
                 }
                 else
                 {
                     startingCollider = blueStartingCollider;
                     Vector3 pos2 = startingCollider.transform.position;
                     pos2.x = pos2.x - 5;
                     startingCollider.transform.position=pos2;
                 }
 
                 // Create a game object reference of the copy to transform its parent
                 // We use 2 cameras in the game we have to put it in the right one
                 GameObject ob=(GameObject) Instantiate(unit,new Vector3(startingCollider.transform.position.x,13,0),unit.transform.rotation);
                 ob.transform.parent = parentObject.transform;
 
                 if(PlayerScript.playerTurn==2)
                  ob.transform.Rotate(new Vector3(0, 180, 0));


There are some more "ifs" in it though. So obviously there are better solutions

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Turret not spawning fireball via instantiation in Unity 2d? 0 Answers

Help with enemy AI Please! (on 2D Platformer) 0 Answers

Enemy Spawner Help 0 Answers

How to automatic reference another GameObject Transform? 1 Answer

Enemy is shooting the opposite direction of player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges