• 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 /
  • Help Room /
avatar image
0
Question by Wampster · Nov 14, 2018 at 09:58 PM · quaternions

How can I rotate my 'shark' vertical?

I am trying to control a shark. I am able to get it moving just how I want and rotating more or less how I want. But, after processing user input, I want the shark to be vertical. But, I can not figure out how to set the z rotation to 0.0f after the input has been processed.

 using UnityEngine;
 
 public class Shark : MonoBehaviour
 {
     [SerializeField]
     private float rotationSpeed;    
 
     private float inputHorizontal;
     private float inputVertical;
 
     private void Update()
     {
         inputHorizontal = Input.GetAxisRaw("Horizontal");
         inputVertical = Input.GetAxisRaw("Vertical");
 
         ProcessInput(inputHorizontal, inputVertical);
 
         RollVertical();
     }
 
     private void ProcessInput( float horizontal, float vertical)
     {
         Quaternion currentRotation = this.transform.rotation;
         float horizontalRotation = horizontal * rotationSpeed * Time.deltaTime;
         float verticalRotation = vertical * rotationSpeed * Time.deltaTime;
         Vector3 rotation = new Vector3(verticalRotation, horizontalRotation, 0);
         Quaternion newRotation = currentRotation * Quaternion.Euler(rotation);
 
         this.transform.rotation = newRotation;
     }
 
     private void RollVertical()
     {
         Quaternion roll = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0.0f);
         transform.rotation = roll;
     }
 }
Comment
Add comment · Show 3
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 Wampster · Nov 15, 2018 at 08:41 AM 0
Share

In each frame, I just want to reset the z rotation back to 0.0f. I've never been very good with Quaternions and I think this is why. In the inspector, if I just put 0 in the z field of the transform rotation, I see the end result that I want. I just can't work out how to make it happen in code.

avatar image Wampster · Nov 16, 2018 at 01:44 PM 0
Share

Is nobody seeing this question or does nobody know hot to set the 2 rotation to 0.0f?

avatar image Wampster · Nov 17, 2018 at 08:56 PM 0
Share

I am still trying to work out how to set an axis to 0 rotation, if anyone knows. Thank you.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Child object looktowards waypoint 0 Answers

Turning the camera 0 Answers

Get model of circle with specyfic angle 1 Answer

Limit rotation within a volume 0 Answers

Rotate single axis of object to nearest 90 degrees 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges