Home Game Development unity – How do I exploit Transform.RotateRound to regularly rotate my GameObject round a brand new pivot within the shortest path?

unity – How do I exploit Transform.RotateRound to regularly rotate my GameObject round a brand new pivot within the shortest path?

0
unity – How do I exploit Transform.RotateRound to regularly rotate my GameObject round a brand new pivot within the shortest path?

[ad_1]

Given a normalized Vector2 or eulerAngles.z, I’m making an attempt to regularly rotate a 2D field round a brand new pivot (the highest of the field) utilizing Transform.RotateRound. My difficulty is that I do not know find out how to cease rotating as soon as the specified rotation has been reached, nor do I perceive how to determine the path of rotation in order that the field rotates the shortest quantity.

Right now my field appears to overshoot the rotation more often than not, usually rotating without end; and the rotation “path” is inconsistent, typically rotating the field “backwards,” almost 360 levels, to achieve the given rotation worth.

utilizing UnityEngine;

[RequireComponent(typeof(BoxCollider2D))]
public class RotationTest : MonoBehaviour
{
    [SerializeField]
    personal float targetRotationZ = 45f;
    [SerializeField]
    personal float rotationSpeed = 200f;

    personal BoxCollider2D boxCollider;

    personal void Awake()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    personal void Update()
    {
        Rotate();
    }

    personal void Rotate()
    {
        // The rotation pivot is now the highest of the field
        Vector2 newPivot = rework.place + rework.up * boxCollider.bounds.extents.y;

        Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, targetRotationZ));

        if (rework.rotation == targetRotation)
            return;

        rework.RotateRound(newPivot, Vector3.ahead, rotationSpeed * Time.deltaTime);
    }
}

To be extra particular about my final use case: I need to rotate my character round a brand new pivot primarily based on the averaged normals of the bottom under it, which I collect from a number of raycasts. Before trying to make use of Transform.RotateRound, I used to be rotating across the default pivot, like this:

Quaternion targetRotation = Quaternion.LookRotation(Vector3.ahead, someNormal);
rework.rotation = Quaternion.RotateTowards(rework.rotation, targetRotation, rotationSpeed * Time.deltaTime);

How can I adapt Transform.RotateRound to realize outcomes much like this? Perhaps there’s additionally a extra applicable manner to do that about which I’m not conscious.

By the best way, I did write a customized operate that I consider does what I need, however would nonetheless prefer to know find out how to obtain this with Transform.RotateRound because it does look like Unity meant that operate for use in a state of affairs like this.

public void Rotate(Vector2 pivotPoint, Quaternion targetRotation)
{
    Vector2 offset = rework.InverseTransformLevel(pivotPoint);

    rework.rotation = targetRotation;

    Vector2 localPivotToWorld = rework.TransformLevel(offset);
    rework.Translate(pivotPoint - localPivotToWorld, Space.World);
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here