- Home /
Rotating a 2D GameObject on Z regardless of the distance.
Hello Unity Answers and happy new year to all,
I have a GameObject which I move on mouse click. The movement of the GameObject depends on mouse click position. Now a user can click anywhere on the screen and the GameObject will move to the click position, but I want the GameObject to move to that point with a 360 degree rotation. No matter how much distance it has to travel it should be only 360 degrees rotation. Please look at the image below. Hope it might clear the idea.
![alt text][1]
As you can see in the ugly image above, no matter the distance the Object has to travel it should reach the target point with exact 360 degree rotation. I hope I have made my problem clear.
Thanks in advance for the help. [1]: /storage/temp/130330-untitled-1.png
Answer by Mehul-Rughani · Jan 01, 2019 at 11:44 AM
Hello, Have you tried Coroutine? if not please try this
public Transform target;
public Vector3 targetPosition;
void Start ()
{
StartCoroutine (MoveToPosition (targetPosition, 2));
}
IEnumerator MoveToPosition (Vector3 targetPosition, float time = 1)
{
float i = 0;
Vector3 tempPosition = target.position;
Vector3 tempRotation = target.eulerAngles;
while (i < 1) {
i += Time.deltaTime / time;
target.position = Vector3.Lerp (tempPosition, targetPosition, i);
target.eulerAngles = Vector3.Lerp (tempRotation, new Vector3 (0, 0, 360), i);
yield return 0;
}
}
this Coroutine will move target to targetposition with 360 rotation in time you have passed. Here in start I have passed 2 so it will move object to target position in 2 sec(by default it will be 1 sec) with 360 rotation.
Hope you are looking for this.
Thanks a lot this is exactly what I was looking for. I will have to find a proper speed value and this will work perfect. THAN$$anonymous$$S!
Answer by Ady_M · Jan 01, 2019 at 12:31 PM
Here's another solution if you know how fast the object should move, but you don't care how long it takes.
Once you have the destination, call startMoving, for example: startMoving (new Vector2 (10, 10));
EDIT: Added stopMoving method and improved the code to prevent any action when the initial distance is zero.
public float distanceInitial;
public Vector2 positionInitial;
public Vector2 positionDestination;
public bool isMoving;
public float moveSpeed;
private void Update ()
{
if (isMoving)
{
// Move
transform.localPosition = Vector2.MoveTowards (transform.localPosition, positionDestination, moveSpeed * Time.deltaTime);
float distanceCurrent = Vector2.Distance (transform.localPosition, positionDestination);
if (distanceCurrent > 0.01)
{
// Calculate progress from distance
float progress = 1 - (distanceCurrent / distanceInitial); // 0.0 .. 1.0
// Rotate
transform.localRotation = Quaternion.Euler (0, 0, 360 * progress);
}
else
{
// Destination Reached
transform.localPosition = positionDestination;
stopMoving ();
}
}
}
public void startMoving (Vector2 Destination)
{
positionInitial = transform.localPosition;
positionDestination = Destination;
distanceInitial = Vector2.Distance (positionInitial, positionDestination);
if (distanceInitial > 0)
{
isMoving = true;
}
}
public void stopMoving ()
{
isMoving = false; // Stop moving
transform.localRotation = Quaternion.identity; // Reset Rotation
}
Your answer
Follow this Question
Related Questions
[Help] Object flips around when using transform.Rotate 3 Answers
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
Find left joystick Vector2 away from sprite's up direction. 1 Answer
How to have a gameobject only rotate 180 degrees 3 Answers
Set rotation of a transform 2 Answers