简体   繁体   English

通过触摸拖动Unity Rigidbody2d AddTorque

[英]Unity Rigidbody2d AddTorque with drag by touch

I am trying to spin a wheel by dragging it with touch or mouse. 我正在尝试通过触摸或鼠标拖动来旋转轮子。 I have used this code before but now I am trying to use it on a Rigidbody2d and with touch. 我以前使用过此代码,但现在我尝试在Rigidbody2d上触摸使用它。

I get an error Argument:1 cannot convert UnityEngine.Vector3 to float on both the rb.AddTorque 我收到一个错误Argument:1 cannot convert UnityEngine.Vector3 to floatrb.AddTorque上都rb.AddTorque

public void OnDrag(PointerEventData eventData)
{
    rb.AddTorque(Vector3.up * torque * -Input.GetAxis("Mouse X"));

    rb.AddTorque(Vector3.right * torque * Input.GetAxis("Mouse Y"));
}

According to AddTorque documentation, it should be taking a Vector3 fine, unless you were to pass in three parameters. 根据AddTorque文档,除非您传入三个参数,否则应该罚款Vector3。 But that is for 2019.2. 但这是在2019.2。

I would take a look at it's parameter type to verify (in VS, ctrl+shift+space while cursor is within argument list). 我将看一下它的参数类型以进行验证(在VS中,当光标位于参数列表内时,则为ctrl + shift + space)。 And also make sure that torque is a float. 还要确保扭矩是浮动的。

The official unity docs state that Add Torque for 2D rigidbodies takes in only a float and optionally a force mode like this: 正式的unity文档指出,为2D刚体添加扭矩仅采用浮标,还可以采用以下强制模式:

public void AddTorque(float torque, ForceMode2D mode = ForceMode2D.Force);

You get an error because you are trying to pass in a Vector3 where a float is expected. 您收到错误消息是因为您尝试传递Vector3时期望使用浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM