如何使一个物体旋转的速度快于另一个物体? [英] How do I make an object rotate half as fast as another?

查看:159
本文介绍了如何使一个物体旋转的速度快于另一个物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌齿,用户可以转动旋转吊桥.目前,齿轮和吊桥以相同的速度旋转,如下所示: https://gyazo.com/14426947599095c30ace94a046e9ca21

I have a cog that the user can turn to rotate a drawbridge. Currently I have the cog and the drawbridge rotating at the same rate, like so: https://gyazo.com/14426947599095c30ace94a046e9ca21

这是我当前的代码:

[SerializeField] private Rigidbody2D thingToRotate;
    void OnMouseDrag()
    {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        Vector2 direction = new Vector2(
            mousePosition.x - transform.position.x,
            mousePosition.y - transform.position.y
         );

        transform.right = direction;
        thingToRotate.transform.up = transform.right;

    }

基本上,它获取鼠标的位置,计算出鼠标与吊桥之间的距离,然后根据矢量设置角度.问题是吊桥角度与齿轮相同.

Basically it gets the mouse position, works out the distance between mouse and the drawbridge, then sets the angle based on the vector. The problem is the drawbridge angle is identical to the cogs.

我想要这样,以便当用户转动齿轮时,它只会稍微旋转一下对象,以便用户可以在吊桥关闭之前将齿轮转动几次.

I want it so that when the user turns the cog it only turns the object a little bit, so the user can turn the cog a few times before the drawbridges closes.

我尝试增加吊桥的欧拉角.我尝试将吊桥旋转设置为齿轮旋转,然后将该旋转除以2.

I've tried adding to the drawbridges euler angle. I've tried setting the drawbridges rotation to the cog rotation and dividing that rotation by 2.

是否具有根据其他角度设置角度的功能?甚至是将旋转设置为比率或其他比率的方法?

Is there a function to set an angle based on another angle? Or even a way of setting rotation as a ratio or another?

推荐答案

您需要计算当前方向和拖动方向之间的角度,再乘以您的比例(即0.5),然后将其转换为矢量

You'll need to calculate the angle between the current orientation and the drag direction, multiply that by your ratio (i.e. 0.5), than convert that into a vector.

float ratio = 0.5f;

float ang = ratio * Mathf.Deg2Rad * Vector2.Angle(transform.right, direction);

thingToRotate.transform.up += new Vector2(Mathf.Cos(ang), Mathf.Sin(ang));

这会使您的代码看起来像:

This would make your code look like:

public float ratio = 0.5f;

[SerializeField] private Rigidbody2D thingToRotate;
void OnMouseDrag()
{
    ...

    float ang = ratio * Mathf.Deg2Rad * Vector2.Angle(transform.right, direction);

    thingToRotate.transform.up += new Vector2(Mathf.Cos(ang), Mathf.Sin(ang));

    transform.right = direction;
}

这篇关于如何使一个物体旋转的速度快于另一个物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆