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
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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

C# 2d Always Rotate Facing Gameobject 1 Answer

Rotation weirdly affects translation 0 Answers

Help with translating and rotating a camera 2 Answers

making my space ship rotate then move. 0 Answers

First person controller not moving properly after rotation 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