• 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
0
Question by ROOOOCKSTAR · Sep 29, 2014 at 03:49 PM · rotationtrajectorytranslationfacing

Object moving and facing center

Hi guys, I am currently doing a 2D game for Android. Let's say I would like Rockets to spawn a bit everywhere, and when they spawn, they face towards the center of the screen (0,0) and they move slowly towards it.

My Rocket is a sprite which faces left.

I have a script which is in charge of spawning them in a random location.

I thought: First I have to rotate the sprite in order to make the left (direction of the rocket) facing the center. So I wrote it in its Start function:

     void Start() {
     
         Vector3 vec = new Vector3 (-transform.position.x, -transform.position.y, 0f).normalized;
     
             angle = Vector3.Angle (Vector3.left, vec);
     
         if (transform.position.y > 0)
             transform.Rotate (Vector3.forward, angle, Space.Self);
         else
             transform.Rotate (Vector3.forward, -angle, Space.Self); 
         }

When I launch the game, I can see that Rockets are well oriented. In the update function, to go towards the center I wrote this:

 void Update () {
     float random1 = Random.Range (0.02f, 0.06f);
     float random2 = Random.Range (0.02f, 0.06f);

     Vector3 vec = new Vector3 (-transform.position.x, -transform.position.y, 0f).normalized;
     Vector3 tr = new Vector3 (vec.x * random1, vec.y *random2, 0f);
     this.transform.Translate (tr);

}

If I don't use the rotation, it works. I mean rockets are going to the center even if not well oriented. My problem is that when I activate both Rotation & Translation, they are doing weird things like circles and I can't figure out why. I mean, I am supposed to have done the rotation on start (so only once), and then only for every update, translating my sprite along the vector to the origin.

What am I doing wrong?

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
0
Best Answer

Answer by robertbu · Sep 29, 2014 at 04:37 PM

Before anyone can give you accurate advice, you need to define clearly the behavior you want for your rockets. Do you want them to start facing the origin, or turn to the origin over time? What are you attempting to do with random1 and random2? Does the object need to collide with other objects, etc. One error jumps out. Your 'vec' is being calcuated as a world direction, but transform.Translate() is local. Try changing to:

 transform.Translate(tr, Space.World);
Comment
Add comment · Show 1 · 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 ROOOOCKSTAR · Sep 29, 2014 at 05:40 PM 0
Share

Well, sorry for that. I wanted them to face the origin (they spawn out of the screen) when they spawn, and only then update their position. About random1 & random2, I made a mistake, I wanted to do some random speed, but I needed only one random. And thanks your answer solved the problem, I didn't know that Translate was local. thanks a lot mate.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

orienting player in one direction 1 Answer

How to get an object to face in the direction that it is traveling? 1 Answer

How do I make sure a object faces a certain direction? 1 Answer

How to move an object a fixed distance along one of its axis 1 Answer

how can i substract one axis by 180? 2 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