0
\$\begingroup\$

I am currently using

transform.RotateAround(transform.position, Vector3.forward, degreesPerSecond * Time.deltaTime) 

to make an object rotate around another object. This works well, but I want to make the object stop rotating around and instead move in a certain direction at the same speed that it was rotating. However, I can not figure out how to convert the degreesPerSecond into a directional speed.

My goal is to have something like this:

if(rotating) {
    transform.RotateAround(transform.position, Vector3.forward, degreesPerSecond * Time.deltaTime)
} else {
    transform.position += transform.right * DegreesToDirectionalSpeed(degreesPerSecond ) * Time.deltaTime;
}

float DegreesToDirectionalSpeed(int degrees) {
    // Code that I don't know goes here
}

Any tips on how to achieve this?

\$\endgroup\$
1
\$\begingroup\$

Romen's answer is correct regarding the physics concepts involved.

In terms of implementation in Unity, you can do it like this:

Transform center;
float degreesPerSecond;
Vector3 axis = Vector3.forward;
bool hasReleased;
Vector3 releaseVelocity;

// Call this when you want to let the sling loose to fly in a straight line.
public void Release() {
    Vector3 offsetFromCenter = transform.position - center.position;
    float radius = offsetFromCenter.magnitude;

    Vector3 travelDirection = Vector3.Cross(axis, offsetFromCenter).normalized;

    releaseVelocity = radius * degreesPerSecond * Mathf.Deg2Rad * travelDirection;

    hasReleased = true;
}

void Update() {
    if(!hasReleased) {
        transform.RotateAround(center.position, axis, degreesPerSecond * Time.deltaTime);
    } else {
        transform.Translate(releaseVelocity * Time.deltaTime, Space.World);
    }
}
\$\endgroup\$
1
\$\begingroup\$

You are looking for something called the tangential velocity vector.

A point on a rotating object at r distance away from the center of rotation will be moving a speed equal to r * ω
Where ω is the angular velocity of the object in radians per second.

If you can determine the distance between the entity and the point it is rotating around you simply multiply the angular speed by that distance.
Note that this only calculates the speed of the object, its direction will be determined by the tangent to the circle at that point.

An explanation of angular velocity can be found here:
http://hyperphysics.phy-astr.gsu.edu/hbase/rotq.html

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.