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 foress89 · Jan 14, 2014 at 06:37 PM · rotate objectrelative rotation

Rotate & Translate

Hello, this is my first question on Unity Awnsers, I usually find what I need searching, but now I am puzzled. I will not show you any code since I am not satisfied with what I have, and I am looking for information rather than a turn-key solution.

Of course I have tried _transform.Translate and _transform.Rotate, I have played with Space.World and Space.Self (maybe I've missed something?)

Here is my goal, I need a circle (it's in a 2d game, an image of a circle with a sphere collider adjusted to its size), that will rotate (but not on itself, around a fixed point not to far from itself), and while it rotates, it should translate in a sort-of random direction.

Of course, any direction will do for starters. My friend DID find a solution but I don't like it. I cannot believe that it's the only way. I'm trying to clean the code and scenes to have only necessary prefabs and scrips.

GameObject>MoveCircleParent> MoveCircleChild

You see where I'm going? The Parent would move in circle and the child would translate or the other way around.

So if anyone can tell me what I should be looking at? Should I use different unity functions? Should I use a coroutine? Or should I leave it the way it is with a parent & child GameObject?

Thank you any help is much appreciated.

After reading a few more posts I decided to include some simple code

 using UnityEngine;
 using System.Collections;
 
 public class MoveTest : MonoBehaviour {
 
     Transform _transform;
     float speed;
     float rotate;
 
     void Awake () {
         _transform = GetComponent<Transform>();
         speed = 2;
         rotate = 2;
     }
     
     void Update () {
         _transform.Translate(new Vector3(0,speed,0) * speed * Time.time);
         _transform.Rotate(0,0,rotate);
     }
 }

Now this doesn't work, I played with it all night and I was able to get the object rotating around a point sorta, but not to translate through the world. This is a "2D World", x is horizontal y is vertical so object can rotate on z axis.

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

Answer by robertbu · Jan 14, 2014 at 06:47 PM

Line 17 should use Time.deltaTime and deltaTime should be used in your Rotate().

 _transform.Translate(new Vector3(0,speed * Time.deltaTime,0)); 

There are a few other simplifications you can make to your code:

 using UnityEngine;
 using System.Collections;
  
 public class MoveTest : MonoBehaviour {
  
     Transform _transform;
     float speed = 2.0f;    // Units per second
     float rotate = 45.0f;  // Degrees per second
  
     void Awake () {
        _transform = transform;
     }
  
     void Update () {
        _transform.Translate(new Vector3(0,speed * Time.deltaTime,0));
        _transform.Rotate(0,0,rotate * Time.deltaTime);
     }
 }

Note this just fixed the code you posted, it doesn't give you some of the other things you list like: - "(but not on itself, around a fixed point not to far from itself" - "should translate in a sort-of random direction."

Use an empty game object as a parent offset from your visible game object. Then you can use 'Transform.RotateAround()' for the first point. Translation code would be applied to the parent. Random walking (the second point) can be interpreted/implement in many ways. Probably best to open a new question just for this issue and give a very detailed description of the kind of random movement you want.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Child Sprite Skewing 1 Answer

Rotating sprite towards another sprite 1 Answer

Play movement and restrictions - Similar to a window wiper 2 Answers

Rotate object via script like in editor 1 Answer

Lerp Rotation broken 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