- Home /
Rolling a capsule lengthways, sideways and twisting with AddTorque
Hi, I need to roll a capsule along its length, like tumbling it, but I also want it to roll sideways and twist. I have got the twisting (rotating with world Y) and side rolling (rotating local Z), so that's all fine, I can roll it around, and twist it. Problem comes when I want to tumble it.
Update: Here is the project file: Download the project file (unity 2019)
(Hold in W or S and you'll see what I mean. I want it to continually tumble.)
I kind of want it to tumble its local X axis, but that of course changes when it rolls. I have to use ApplyTorque too btw.
I tried getting the forward vector, rotating that 90 then flattening it, which kind of works, but when it gets to vertical, it flips, so wants to rotate the other way, ending up with the cylinder just standing up. I want it to continually 'tumble'.
Here's a graphic illustrating the problem. I want to be able to apply torque to the orange rotation direction regardless of the X and Y rotation.
So which ever way the cylinder is 'facing' I want it to tumble over and over, but also be able to roll and twist it.
The left cylinder in the image is the kind of 'default' - so if I press A/D it twists (rotates on Y), Q/E it rolls (rotates on Z) and W/S to tumble (rotate on X). But I need that to happen whichever way the cylinder is 'facing'.
Here's the code that kind of works, but it just stands up as the ApplyTorque cancels itself out when the axis flips.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMover : MonoBehaviour
{
private Rigidbody capsuleRb;
public float sideRollSpeed = 60f;
public float forwardRollSpeed = 50f;
public float twistSpeed = 50f;
private ForceMode forceMode = ForceMode.Acceleration;
void Start()
{
capsuleRb = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 lineStart = transform.position;
Vector3 sideAxis = Quaternion.AngleAxis(-90, Vector3.up) * transform.forward;
sideAxis = Vector3.ProjectOnPlane(sideAxis, Vector3.up);
sideAxis = sideAxis.normalized;
Debug.DrawLine(lineStart, lineStart + (sideAxis * 2f), Color.blue, Time.deltaTime);
if (Input.GetKey(KeyCode.D)){
capsuleRb.AddTorque(Vector3.up * forwardRollSpeed,forceMode);//twist
}
if (Input.GetKey(KeyCode.A)){
capsuleRb.AddTorque(-Vector3.up * forwardRollSpeed,forceMode);//twist
}
if (Input.GetKey(KeyCode.E)){
capsuleRb.AddTorque(-capsuleRb.transform.forward * sideRollSpeed,forceMode);//roll
}
if (Input.GetKey(KeyCode.Q)){
capsuleRb.AddTorque(capsuleRb.transform.forward * sideRollSpeed,forceMode);//roll
}
if (Input.GetKey(KeyCode.W)){
capsuleRb.AddTorque(-sideAxis * twistSpeed,forceMode);//tumble
}
if (Input.GetKey(KeyCode.S)){
capsuleRb.AddTorque(sideAxis * twistSpeed,forceMode);//tumble
}
}
}
Answer by mrwaveydavey · Jun 14, 2019 at 12:12 AM
After a lot of trial and error, I've solved it. There were 2 main problems. 1) how to keep the forward vector forward, even when the object has flipped. 2) It all needs to be relative to something, like forwards in relation to the camera for example.
So the hours of trial and error has ended up with more or less one line of code!:
float signedAngle = -Mathf.Sign(Vector3.Dot(Vector3.right, transform.forward));
This basically gives 1 or -1 depending on if the object is 'flipped'.
So now, on key down, I just get the side Axis vector as in the example and set that as the rotation angle. On key up, I check if it is flipped, and if so, invert the axis next time. So now it will always rotate the same way, even if its upside down on either the other 2 axis.
Your answer
Follow this Question
Related Questions
Rotation gameobject 0 Answers
How would I interpret angular displacement from target rotation to max out at 180°? 3 Answers
Control object yaw by two axis 1 Answer
Get angle around a specified axis. 1 Answer
C# - Rotation around object axis 2 Answers