- Home /
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.
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.
Your answer
Follow this Question
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