• 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 chilpert · Apr 18, 2020 at 02:07 PM · rotationtransformgyroscopequaternions

Headlight rotation issue when moving the camera

Hello,

In my game the first player controls the camera using the keyboard. He can make the camera look up and down as well as left and right. The second player uses a phone with a gyroscope to control a headlight. The headlight is a child of the camera, because it's supposed to be a vehicle and a vehicle's headlight always turns with the vehicle itself. Driving straight is not a problem, but as soon as the first player takes a turn, suddenly the gyroscope headlight controller stopps working correctly. Left and right seems to be fine at any time, but up and down gets reversed. Since the code that applies the rotations is scattered in multiple scripts, I will try to give you a short version.

This is the code that rotates the camera (first player):

 if (Input.GetKey("up") || Input.GetKey("w"))
   transform.Rotate(-speedControls * Time.deltaTime, 0f, 0f);
 
 if (Input.GetKey("down") || Input.GetKey("s"))
   transform.Rotate(speedControls * Time.deltaTime, 0f, 0f);
 
 if (Input.GetKey("left") || Input.GetKey("a"))
   transform.Rotate(0f, -speedControls * Time.deltaTime, 0f);
 
 if (Input.GetKey("right") || Input.GetKey("d"))
   transform.Rotate(0f, speedControls * Time.deltaTime, 0f);


This is the code related to the gyroscope (second player):

 // set the headlight's rotation to the gyroscope's rotation
 headlight.transform.rotation = phoneRotation;
 
 // make sure that the headlight is rotating with the camera / vehicle
 headlight.transform.Rotate(-cam.transform.localRotation.eulerAngles);


The first player's input will be handled first, then comes the gyroscope input. The gyroscope input entirely overwrites the first player's input, as seen in the first line. That's why I have added the second line, which is supposed to redo the first player's input.

Comment
Add comment · Show 2
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 TotalPixel · Apr 18, 2020 at 02:30 PM 0
Share

Have you tried using world rotation ins$$anonymous$$d of local?.

avatar image chilpert TotalPixel · Apr 18, 2020 at 04:59 PM 0
Share

Yes, I have tried that as well. The result is very confusing to me. Up and down seems to be not working at all anymore.

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

190 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Rotating a Vector direction by another Vector direction in one axis 1 Answer

Getting the actual object rotation on just 1 axis 1 Answer

Rigidbody spins out of control when colliding into corner 0 Answers

use quaternions in unity to rotate object. 1 Answer

Min-Angle Constraint Script Causes Jitter 0 Answers

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