Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by t94j0 · Jun 05, 2017 at 02:16 AM · cameracamera-movementfirst-person-controllerfirst person controller

Trouble Implementing First Person Camera

I am trying to implement an FPS camera, but my rotation is broken for some reason. I am attaching this script on a camera called FPSCamera which is a child of the Player object tagged "Player". I used Debug.Log to make sure rotation{X,Y} was working properly, and it was printing out numbers. I want to move the player's rotation when moving in the X direction, and for the camera to move up and down when the mouse moves in the Y direction.

I am pretty new to this, so if my code looks off outside of the question, please critique me for that as well.

 public class FpsCamera : MonoBehaviour {
     private Quaternion playerRotation;
     private Quaternion cameraRotation;
 
     public float sensitivity = 100.0f;
     void Start () {
         cameraRotation = gameObject.GetComponent<Transform>().rotation;
         playerRotation = GameObject.FindGameObjectWithTag("Player").transform.rotation;
     }
     
     void Update () {
         float rotationX = playerRotation.eulerAngles.x + (Input.GetAxisRaw("Mouse X") * sensitivity);
         float rotationY = cameraRotation.eulerAngles.y + (Input.GetAxisRaw("Mouse Y") * sensitivity);
 
         cameraRotation = Quaternion.Euler(0f, rotationY,  0f);
         playerRotation = Quaternion.Euler(rotationX, 0f, 0f);
     }
 }


EDIT: I forgot to mention, that I checked {camera,player}Rotation and they both were not equal to null

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
0

Answer by Glurth · Jun 07, 2017 at 05:26 PM

I see that you assign your computed rotation to the private variable cameraRotation, but I do NOT see you actually assigning it to the camera's transform's rotation: e.g.

 gameObject.transform.rotation= cameraRotation;

Until you actually assign it to the object's transform, no changes will be visible. (Same for the player object.)

Comment
Add comment · Show 3 · 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 t94j0 · Jun 07, 2017 at 06:20 PM 0
Share

Great! I'll try this when I get home. So, when you get the component with GetComponent, it doesn't automatically update the object? I just assumed it would since you can use GetComponent on a Transform and it will update the position automatically.

avatar image Glurth t94j0 · Jun 07, 2017 at 06:28 PM 0
Share

When you get a component, you are getting a REFERENCE instance of a CLASS. When you modify it's values, you are doing it THROUGH the reference, modifying the original object.

Quaternions (and Vectors) however, are structs. This means when we access it, we are accessing the VALUE directly, not via a reference. Just like an int, or a float.

This is an important c# concept: https://msdn.microsoft.com/en-us/library/4d43ts61(v=vs.90).aspx (or google search for "c# reference vs value")

avatar image t94j0 Glurth · Jun 07, 2017 at 09:35 PM 0
Share

Ahh, that makes sense. I didn't know quaternions/vectors were structs. Thanks!

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

144 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

Related Questions

Transform.Rotate x and y rotate z, where z is 0 0 Answers

camera force scaling child objects SOLVED 1 Answer

Camera can't keep up with mouse when the FPS is low 0 Answers

FPS Weapon Clipping 1 Answer

Tilting Head with Camera from the FirstPersonController Script FPS controller provided in (Unity 5) 2 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