Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Dec 26, 2013 at 10:21 PM by zombience for the following reason:

solved in comments

avatar image
0
Question by zombience · Dec 26, 2013 at 07:32 PM · quaternionangleclamped rotationrelative rotation

Quaternions, relative clamped rotation, look rotations in spaceship movement

I've come up against my limited knowledge of Quaternions, and I've almost solved the problem, but not quite.

In the code below, the pitch and roll of a ship work exactly as expected, EXCEPT that when rotation a "direction" transform that determines the forward direction of the ship, the rotations are then additive, so rotating the "direction" of travel 90 degrees yields a 180 degree rotation in the ship.

The ship must be constrained to one direction of travel, as if it were in a tunnel. the tunnels may change direction, but the ship's rotation must remain relative to the tunnel's main direction.

Here's the code below. It all works as I want, except for additive rotations between direction and final forward direction of the ship:

Thanks in advance!

 using UnityEngine;
 using System.Collections;
 
 public class QuatTest : MonoBehaviour {
 
     public Transform direction;
     public float roll;
     public float rollSpeed;
     public float pitch;
     public float rotationSpeed;
 
     protected Transform trans;
     public Transform container;
 
     protected Quaternion rollQuat = Quaternion.identity;
     protected Vector3 travelDir;
 
 
 
     void Start () 
     {
         trans = transform;
         container = trans.parent;
     }
     
 
     void Update () 
     {
         container.rotation = direction.rotation;
         travelDir = container.forward;
         Rotation();
 
         roll += rollSpeed;
         roll = Mathf.Repeat(roll, 360);
 
         Debug.DrawRay(trans.position, trans.forward * 10f, Color.green);
         Debug.DrawRay(trans.position, travelDir * 10f, Color.white);
         Debug.DrawRay(trans.position, trans.right * 10f, Color.red);
     }
 
     protected void Rotation()
     {
         //this line ALMOST acts as desired:
         // without LookRotation, AngleAxis yields completely wrong angles and ship rotates wildly
         // with LookRotation as is, rotation "direction" transform creates an ADDITIVE rotation between the two,
         // so rotating the "direction" transform 90 degrees yields a 180 degree rotation in the ship, BUT
         // the pitch and roll then behaves as expected on the resultant travelDir and trans.right vectors
         // i've tried replacing travelDir with trans.forward, and this yields the same result
         rollQuat = Quaternion.AngleAxis(roll, travelDir) * Quaternion.LookRotation(travelDir); 
 
         trans.rotation = Quaternion.Lerp(
             trans.rotation, 
             Quaternion.AngleAxis(pitch, trans.right) * Quaternion.LookRotation(travelDir) * rollQuat,
             Time.fixedDeltaTime * rotationSpeed);     
     }
 }
Comment
Add comment · Show 4
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
avatar image sparkzbarca · Dec 26, 2013 at 08:22 PM 1
Share

im not quite understanding are you saying when you pitch 90 degrees to aim at the ground you end up with 180 pitch ins$$anonymous$$d?

also I feel like lookrotation should be

Lookrotation(travelDir, ...) with ... being maybe container.up or transform.up i'm not sure

what is it exactly that you want to do?

Cause I also feel like what your wanting to do is

take the current rotation add to it the pitch and the roll required

but if thats the case then replace quat.look with the ships current rotation so

trans.rotation rollquat pitchquat;

Of course I think if you do a look direction in the ships forward AND use the ships up (so lookrotaiotn(travelDir,trans.forward) you end up with the ships rotation as it is.

avatar image zombience · Dec 26, 2013 at 10:08 PM 0
Share

well, pitch and roll are working currently, and they work properly to the ship's rotation. however, that only works if the

Vector3 travelDirection (deter$$anonymous$$ed by public Transform direction.rotation)

is Vector3.zero.

In order to overcome that and make pitch and roll relative, I am trying to use "travelDirection" as a "forward" axis to orient the rotations. but, because of the additive nature of the rollQuat Quaternion specifically, when I rotate the "forward" axis travelDirection 90 degrees, the ship turns 180.

In its new 180 degree orientation, the roll and pitch work exactly as I desire, but the ship is just facing the wrong way. I have tried various combinations of LookDirection(), AngleAxis(), and other quaternion functions. this is the closest I've come, except for the pesky wrong orientation.

I will try adding the ship's current to roll and pitch, although I think I tried that many iterations ago. I've changed it so many times now that I can't quite remember what I"ve tried and what I haven't. :0

avatar image zombience · Dec 26, 2013 at 10:18 PM 0
Share

using this line:

 rollQuat = Quaternion.AngleAxis(roll, travelDir) * Quaternion.LookRotation(travelDir); 
         trans.rotation = rollQuat * Quaternion.AngleAxis(pitch, trans.right) * Quaternion.LookRotation(travelDir);

yields the same behavior. if I reorient the "direction" transform, the ship rotates twice as far.

avatar image zombience · Dec 26, 2013 at 10:22 PM 0
Share

oh man, i realized i had AngleAxis in the logic twice, which was obviously the problem.

i've been looking at this problem way too long. if you want to put your comment as an answer, i'll gladly accept it.

thanks for the extra set of eyes on the problem!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Set a rotation relative to position between two objects. 0 Answers

Quaternion lerp is not rotating my object to the desired rotation? 1 Answer

Finding the angle between 2 clicked points 2 Answers

problems with quaternion rotations 4 Answers

Coroutine with Quaternion.Angle 1 Answer


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