Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mrwaveydavey · Jun 06, 2019 at 05:45 PM · rotationaxisrotation axis

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.

alt text

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
         }
     }
 }
   


rollingcapsule.jpg (63.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges