Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by DarkNinja88 · Jun 26, 2015 at 11:55 PM · unity 52d game

How do I move a 2d object to center of screen?

public float fallSpeed = 100.0f;

 void Update() 
 {
     
     transform.Translate(Vector3.down * fallSpeed * Time.deltaTime);
     
 }



I currently have this code making my object fall down and it goes off the camera screen, I then want it to move to the center of the screen to which then it will activate a particle explosion. How would I go about doing that?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · Jun 27, 2015 at 12:06 AM

You can use Camera.ViewportToWorldPoint and pass it a Vector3 with x and y values = 0.5 (middle of viewport). The z values would determine the distance from the camera.

Comment
Add comment · Show 7 · 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
avatar image DarkNinja88 · Jun 27, 2015 at 12:15 AM 0
Share

thanks, is there a way i could speak to you more about this? a little confused on how to make this translate in code.

avatar image Cherno · Jun 27, 2015 at 09:32 AM 0
Share

Let's say you have a GameObject go that you want to put in the center of the screen.

 public GameObject go;
 float distanceFromCamera = 10f;
 
 void Start() {
      Vector3 centerPos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, distanceFromCamera));
      go.transform.position = centerPos;
 }
avatar image DarkNinja88 · Jun 27, 2015 at 09:36 AM 0
Share

ah ok, awesome thanks! Now how would i go about activating the particle portion?

avatar image Cherno · Jun 27, 2015 at 04:19 PM 0
Share

Does the object already have a ParticleSystem? If yes,set it to Play on Awake = true, and deactivate the ParticleSystem component. Then, after centering the gameobject, enable it by code:

 go.GetComponent<ParticleSystem>().enableD = true;

Or, you could set it to Play on Awake = false, and use ParticleSystem.Emit to emit particles after centering the gameobject.

  go.GetComponent<ParticleSystem>().emit(20);


avatar image DarkNinja88 · Jun 27, 2015 at 07:35 PM 0
Share

i created the particle, and its checked to play on awake in the inspector. I should uncheck this before coding it in you mean?

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Platformer Pick up and move object script help! 1 Answer

Best Image Formats, Sizes, and Resolutions for Unity Sprites? 2 Answers

Unity doesn't open mi project 0 Answers

How can I create force on one end of an object using mouse dragg whose other end is fixed 1 Answer

waypoint stuck at the first waypoint 3 Answers


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