简体   繁体   中英

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.

I get an error Argument:1 cannot convert UnityEngine.Vector3 to float on both the 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. But that is for 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). 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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