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 /
avatar image
0
Question by popuppirate · Nov 19, 2014 at 08:09 PM · rotationrigidbodyquaternionforward

Keeping transform.forward facing forwards

Hi guys,

I've been wrestling with a problem involving a physics movement script. The script is great, but I recently added in being able to look up and down using torque on the rigidbody.

This is also great, though what it means is that the transform.forward is rotated in the x plane (look up/down), which means when I apply a force in that direction to make the player move forward, it does a loop the loop as it tries to follow the player's transform.forward.

I thought I could negate this by applying the opposite Quaternion rotation as shown in my code, but this has seemingly no effect on the loop-the-loop behaviour.

Can anyone help me? (Excuse the code formatting, I extracted all that was relevant I hope)

Cheers,

Popuppirate

 void Update(){
 
 Quaternion rotation = new Quaternion(-transform.rotation.x, 0f, 0f, -1);
 true_forwards = rotation * transform.forward;
 
 currVertThrust = 0.0f;
 float aclVertAxis = Input.GetAxis ("Vertical");
        if (aclVertAxis > deadzone) {
        currVertThrust = aclVertAxis * forwardAcl;
       } else if (aclVertAxis < -deadzone) {
         currVertThrust = aclVertAxis * backwardAcl;
 }
 
 if (somebool) {
 float tiltAxis = Input.GetAxis ("Mouse Y");
 currTilt=Mathf.Clamp (currTilt,-10f, 10f);
      if (Mathf.Abs (tiltAxis) > deadzone) {
      currTilt += tiltAxis;        
 } else {
    currTilt=0.0f;
    } 
 }
 
 void FixedUpdate(){
         
         RaycastHit hit;
         for (int i=0; i<hoverPoints.Length; i++) {
         var hoverPoint=hoverPoints[i];
         if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit, hoverHeight, layerMask)){
         body.AddForceAtPosition(Vector3.up*hoverForce*(1.0f-(hit.distance/hoverHeight)), hoverPoint.transform.position);
     
         } else if(someotherbool) {
             if (transform.position.y>hoverPoint.transform.position.y){
             body.AddForceAtPosition(hoverPoint.transform.up*hoverForce,hoverPoint.transform.position);
             } else {
           body.AddForceAtPosition(hoverPoint.transform.up*-hoverForce,hoverPoint.transform.position);
                 }
             }
     }
 
 
         if (Mathf.Abs (currVertThrust) > 0) {
 
             body.AddForce(true_forwards*currVertThrust);        
         }
 
         if (Mathf.Abs (currHorizThrust) > 0) {
             body.AddForce(transform.right*currHorizThrust);        
         }
 
 
         if (someotherbool) {
                         if (currTurn > 0) {
                                 body.AddRelativeTorque (Vector3.up * currTurn * turnStrength);
                         } else if (currTurn < 0) {
                                 body.AddRelativeTorque (Vector3.up * currTurn * turnStrength);
                         }
                     if (currTilt>0){
                         body.AddTorque (Vector3.Cross (transform.forward, transform.up)*currTilt*turnStrength*0.2f);
                     }
                     else if (currTilt<0){
                         body.AddTorque (Vector3.Cross (transform.forward, transform.up)*currTilt*turnStrength*0.2f);
                     }
                 }
 
 }
 
 
         }

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
1
Best Answer

Answer by DMGregory · Nov 19, 2014 at 08:18 PM

If you just want the "horizontal forward" (ie. the forward direction projected into the world's horizontal/xz plane) you can get it like this:

 Vector3 horizontalForward = transform.forward;
 horizontalForward.y = 0f;
 horizontalForward.Normalize();

Or if you want to be able to get any local direction into this horizontal plane, you can do this:

 Quaternion horizontalOrientation = Quaternion.LookRotation(Vector3.up, -transform.forward) * Quaternion.AngleAxis(90f, Vector3.right);
 Vector3 arbitraryVector = horizontalOrientation * new Vector3(rightwardAmount, 0f, forwardAmount);

Comment
Add comment · Show 2 · 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
avatar image popuppirate · Nov 19, 2014 at 08:42 PM 0
Share

Thanks D$$anonymous$$G,

I tried both methods and my object, while not flipping over, still gains upwards momentum. I want purely lateral momentum. Any thoughts as to why this might be?

avatar image popuppirate · Nov 19, 2014 at 09:59 PM 0
Share

For whatever reason my script was not updating- now it is and it works great! Cheers!

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

27 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

Related Questions

look at movedirection 1 Answer

why can't my Rigidbody not rotate on slopes? 1 Answer

Rigidbody.addforce then rigidbody.rotation and movement along transform.forward 2 Answers

Rotate Rigidbody to always face a force 0 Answers

transform.Rotate and Rigidbody.MovePosition 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