1
\$\begingroup\$

I'm faced with the following problem:

I'm trying to implement a rigidbody-based movement for my submarine game which means I want (need?) to useAddForce() and AddTorque() in order to get proper collision detection.

Currently I'm achieving the desired rotational behavior by quaternion multiplication/rotation.

But with this approach, the submarine bugs through the terrain. To avoid that it seems my only option is to use a rigidbody.

Here's a video illustrating the problem.

On the left is the correct behavior. I want to keep the nose pointing where it has been tilted at and not rotate around a fixed axis, Vector3.up in this case.

How can I calculate the correct input for AddTorque(vec3) to get the result I need?

Left attempt with quaternions (what I need):

input2 = Input.GetAxis("Sideways");
transform.rotation =
    Quaternion.Euler(0, input2 * Time.deltaTime * rotation_speed, 0) * transform.rotation;

Right attempt with torque (which doesn't quite work yet):

// in Update()
rotateY = Input.GetAxis("Sideways");

void FixedUpdate() {
    // experimenting with AddTorque() and various inputs didn't
    // get me any further
    rb.AddRelativeTorque(Vector3.up * rotateY * speed);
}

I hope someone can point me in the right direction, thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ Nice demonstration of the issue! I'm not familiar with unity, so not posting an answer but it seems from the documentation that AddRelativeTorque uses the coordinate system of the body, so up means up relative to the body. What happens we you use AddTorque instead, with exactly the same arguments? \$\endgroup\$ – monocell Oct 7 '19 at 1:21
  • \$\begingroup\$ Hey, thanks for your time. I indeed tried several combinations, also the one you suggested. The result is far from what I need. I get equivalent results when doing: rb.AddRelativeTorque(Vector3.up * rotateY * speed); and rb.AddTorque(rb.transform.up * rotateY * rotation_speed); Both rotate my submarine around its local Up Vector. But I can't figure out the required vactor for what i'm trying to do. I think it needs to be changing depending on the overall rotation/tilt, not sure though. edit: why is my code editing not working? \$\endgroup\$ – maxi6655 Oct 7 '19 at 8:21
  • \$\begingroup\$ I'm thinking more like AddTorque(Vector3.up * ..., what happens then? As far as I can see it should not change depending on the overall tilt... \$\endgroup\$ – monocell Oct 7 '19 at 8:36
  • \$\begingroup\$ youtube.com/watch?v=6RwOAS-Bp40 The right movement is correct, the left one is what needs to be fixed. I'm not even sure what causes the left one to rotate around what appears to be local Z, when using Vector3.up. \$\endgroup\$ – maxi6655 Oct 7 '19 at 8:56
  • \$\begingroup\$ Well, at least we are insane together... Sorry I can't be of any help. \$\endgroup\$ – monocell Oct 7 '19 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.