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
1
Question by benandlucy · Nov 03, 2013 at 12:48 AM · cameraplayerdirectionangleseuler

Camera rotating super fast when trying to match the camera and players Y euler angle.

I'm still a noob at this so bare with me. I'm trying to get my player to move in the Camera's direction that it is facing. I heard something about how euler angles can help. But whenever I start the game, the smallest mouse movement makes the camera go insane. Here's my code so far.

 #pragma strict
 
 
 var TheCamera : GameObject;
 
 function Start () {
     Screen.showCursor=false;
 }
 
 function Update () {
 transform.eulerAngles.y= TheCamera.transform.eulerAngles.y;
 
     
 
     if(Input.GetKey(KeyCode.W)){
     transform.Translate(0,0,0.2);
     
         
 
 
 
         
     
         
 }
 }


If you have any other questions just ask me. Thank you.

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 aldonaletto · Nov 03, 2013 at 02:52 AM

First of all, reading eulerAngles is always a bad idea: there are several different XYZ combinations that result in the same rotation, and according to Murphy's laws Unity will return the wrong combination at the worst possible times. The reciprocal isn't true: setting eulerAngles works fine - provided that you never assign a single axis like you're doing in this code: always assign a whole Vector3 to eulerAngles.

Second: if the camera is childed to the player, the code above will drive it nuts (and you too). If the camera isn't a child of the player, then you can get its forward direction, zero Y in order to keep only the horizontal part, and assign it to transform.forward:

 public var speed: float = 10; // add the speed variable
 
 function Update () {
     var dir: Vector3 = TheCamera.transform.forward; // get the camera's forward...
     dir.y = 0; // keep it strictly horizontal
     transform.forward = dir; // player turns in the direction found
     if (Input.GetKey(KeyCode.W)){
         // move at constant speed
         transform.Translate(0, 0, speed * Time.deltaTime);
     }
 }

Notice that I added a speed variable, which's multiplied by Time.deltaTime in order to keep a framerate-independent movement.

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 benandlucy · Nov 03, 2013 at 06:01 AM 0
Share

Thanks bro, worked like a charm! I'm never going to put a camera as a child again.

avatar image aldonaletto · Nov 03, 2013 at 02:01 PM 0
Share

On the contrary: the camera is usually childed to the player in FPS games, like in the First Person Controller standard prefab. The solution is to turn the camera only up and down, and turn the whole character left/right - since the camera is a child of the character, it will turn left/right too. The First Person prefab does this by using two $$anonymous$$ouseLook scripts: one attached to the character that only rotates about world Y, and the other attached to the camera and limited to rotate about local X only.

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

16 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

Related Questions

Euler makes my look script stop... 1 Answer

How to don't face player to camera direction? 1 Answer

Player look in camera direction problem 1 Answer

Weapon not updating position and moving as Player moves 0 Answers

allows users to view 360º content without a VR headset 0 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