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 RoyalJellyfish · Oct 06, 2013 at 12:52 PM · javascripteuleranglestransform.forward

Eulerangles and Transform.forward... something's off?

So, long story short I'm just trying to make a simple first-person-shooter for the moment (adding on stuff to actually make it interesting later on, but not worrying about that for now). I'm sure that a similar question must have been asked some time in the past but I honestly have no idea how to search for my problem. I'm using my mouse's inputs to change the rotation on my camera, and attaching an object for movement. The object's rotation is made to match the camera's rotation, and then when WASD keys are pressed the object moves according to transform.forward and transform.right... however, when I rotate the camera to certain angles sideways, the movements get all screwed up.

I'm sorry if my explanation is bad, if you need to test yourself to tell the specifics of it, the only specifications for this scene is a camera and single object. Thankyou in advance for advance for anyone who can/does help out with this script. I'm sure it's definitely not the most efficient, but I'm just trying to work with what I have.

script for the object

 var movespeed:float = 4;
 var zecamera:GameObject;
 
 function Start () {
 
 }
 
 function FixedUpdate () 
 {
     transform.rotation.y = zecamera.transform.rotation.y;
     if(Input.GetKey(KeyCode.W))
     {
         transform.position += transform.forward * movespeed * Time.deltaTime;
     }
     if(Input.GetKey(KeyCode.D))
     {
         transform.position += transform.right * movespeed * Time.deltaTime;
     }
     if(Input.GetKey(KeyCode.A))
     {
         transform.position += transform.right * -movespeed * Time.deltaTime;
     }
     if(Input.GetKey(KeyCode.S))
     {
         transform.position += transform.forward * -movespeed * Time.deltaTime;
     }
 }

script for the camera

 var playerlel:GameObject;
 var sensitivityY:float = 1;
 var sensitivityX:float = 1;
 var movespeed:float = 4;
 
 function Update ()
 {
     //rotate up/down
     transform.eulerAngles.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
     //rotate left/right
     transform.eulerAngles.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
     transform.position = playerlel.transform.position;
 }
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
2
Best Answer

Answer by robertbu · Oct 06, 2013 at 03:37 PM

You have a couple of related issues here. First from the script reference for Transform.eulerAngles:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

The euler angles you set are not necessarily the ones you get back out. Run this bit of script:

 transform.eulerAngles = Vector3(180,0,0);
 Debug.Log(transform.eulerAngles);

The output is (0,180,180). So imagine you want to set (180, 45, 0), but you set it one axes at a time starting with 'x'. You would get (0, 45, 180)...not even physically the rotation you wanted.

Given your code above, the solution is to maintain your own Vector3 and assign it. Never read the eulerAngles. Something like:

 myRotation.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
 myRotation.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
 transform.eulerAngles = myRotation;


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 RoyalJellyfish · Oct 07, 2013 at 01:24 AM 0
Share

Thanks for the answer, I guess my understanding of what eulerangles are and how they work really need some refining :P. I'll test this out as soon as I can get back to working on it, looks like a pretty solid solution to me though. :D

avatar image RoyalJellyfish · Oct 07, 2013 at 10:33 AM 0
Share

That bit you gave me I think did make my camera's movement smoother, at least probably it won't mess up what I'm planning on doing next. I actually realized that the problem I was having was due to the "player" object that I was trying to attach the camera to (the player object's rotation matches the camera's rotation) and that was making the movement screwy. now that I've figured's that out I can be well on my way :P.

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

15 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

Related Questions

Rotate object around transform.forward 1 Answer

Store initial rotation in JavaScript 2 Answers

Smooth rotation in 90° increments 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Why does my rotation lock? 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