Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 string_username · Mar 05, 2017 at 04:05 PM · c#animationeuleranglescs

transform.eulerAngles not working

It used to work and all of a sudden it didn't. I checked my other scripts and I'm pretty sure I haven't used the players euler angles in any other script.

This is the script I'm using euler angles in:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [DisallowMultipleComponent]
 [RequireComponent(typeof(Animator))]
 [RequireComponent(typeof(Rigidbody))]
 [RequireComponent(typeof(CapsuleCollider))]
 
 public class Movement : MonoBehaviour
 {
     public float DiagonalWalkRotationSpeed;
     public Transform camera;
 
     Animator animator;
     CapsuleCollider capsuleCol;
 
     Vector3 prevRot; //Previous rotation
     Vector3 currRot; //Current rotation
     bool diagMove = false; //Diagonal movement
     bool prevDiagMove; //Previous diagonal movement
     bool rotatedLeft = false;
     bool rotatedRight = false;
     float diagRot = 45; //How much the diagonal rotation is going to be
     float diagDegMoved; //How many degrees the player have rotated for the diagonal movement
 
     private void Awake()
     {
         animator = GetComponent<Animator>();
     }
 
     private void Update()
     {
 
         currRot = new Vector3(currRot.x, camera.eulerAngles.y, currRot.z);
 
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, currRot.y, transform.eulerAngles.z);
 
         Move();
         prevRot = currRot;
         prevDiagMove = diagMove;
     }
 
     void Move()
     {
         //Get axis inputs
         var vAxis = Input.GetAxisRaw("Vertical");
         var hAxis = Input.GetAxisRaw("Horizontal");
 
         //Walking states
         if (vAxis > 0 || hAxis != 0)
             animator.SetFloat("Walking", 1); //If any axis-input is given, start walking
         else if (vAxis == 0)
             animator.SetFloat("Walking", 0); //Else if no axis-inputs are given, stop walking
         if (vAxis == -1)
             animator.SetFloat("Walking", -1); //If backwards input is given, walk backwards
 
         //Direction states
         if (vAxis == 0 && hAxis != 0)
             animator.SetInteger("Direction", hAxis == 1 ? 1 : 3); //If moving horizontally, set direction to either 1 or 3 (Left or right) depending on input
         else if (vAxis == 1)
             animator.SetInteger("Direction", 2); //If moving forward, set direction to 2 (forward)
 
         //Diagonal Movement
         if (vAxis != 0) //If holding forward button
         {
             if (hAxis == 1 && !rotatedRight) //If holding left button
             {
                 diagMove = true; //Player is moving diagonally
                 rotatedLeft = true; //Player is rotated to the left
                 rotatedRight = false; //Player is not rotated to the right
                 if (diagDegMoved < diagRot)
                 {
                     transform.eulerAngles -= new Vector3(0, DiagonalWalkRotationSpeed, 0);
                     diagDegMoved += DiagonalWalkRotationSpeed;
                 }
                 else
                     transform.eulerAngles = currRot + new Vector3(0, -diagRot, 0);
             }
             if (hAxis == -1 && !rotatedLeft) //If holding right button
             {
                 diagMove = true; //Player is moving diagonally
                 rotatedRight = true; //Player is rotated to the right
                 rotatedLeft = false; //Player is not rotated to the left
                 if (diagDegMoved < diagRot)//If the player is not finished rotating
                 {
                     transform.eulerAngles += new Vector3(0, DiagonalWalkRotationSpeed, 0); //Keep rotating the player
                     diagDegMoved += DiagonalWalkRotationSpeed;
                 }
                 else //If the player is finished rotating
                     transform.eulerAngles = currRot + new Vector3(0, diagRot, 0); //Stop at target rotation
             }
         }
 
         if (hAxis == 0 || (hAxis == 1 && rotatedRight) || (hAxis == -1 && rotatedLeft)) //If player is not moving OR Player is changing to the opposite rotation
         {
             if (rotatedRight) //If rotated
             {
                 transform.eulerAngles -= new Vector3(0, DiagonalWalkRotationSpeed, 0); //Rotate back
                 if (transform.eulerAngles.y <= currRot.y)
                     transform.eulerAngles = currRot;
             }
             else if (rotatedLeft) //If rotated
             {
                 transform.eulerAngles += new Vector3(0, DiagonalWalkRotationSpeed, 0); //Rotate back
                 if (transform.eulerAngles.y >= currRot.y)
                     transform.eulerAngles = currRot;
             }
 
 
             if (transform.eulerAngles == currRot) //If finished rotating back
             {
                 //Reset
                 diagDegMoved = 0; 
                 diagMove = false;
                 rotatedRight = false;
                 rotatedLeft = false;
             }
         }
 
     }
 
 }
 

It's a animation-based movement script.

The reason for all the comments is because it's a school assignment.

At line 70 I start using euler angles,

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 Commoble · Mar 05, 2017 at 04:50 PM 0
Share

Can you be more specific about the manner in which they aren't working?

Side note: You can never have too many comments! Never be ashamed of having lots of comments, comments are everybody's friend.

avatar image string_username Commoble · Mar 05, 2017 at 06:04 PM 0
Share

$$anonymous$$y character is supposed to smoothly turn into a diagonal direction but ins$$anonymous$$d instantly changes to the diagonal position.

The if-statements goes through like they should but the euler angles doesn't change except for at line 77 & 91 when DiagDeg$$anonymous$$oved has counted up to target rotation.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Commoble · Mar 05, 2017 at 06:36 PM

You need to account for the fact that the Update function gets called up to 60 times per second. If you have any velocity or acceleration field defined in units per second, or angular velocity defined in radians or degrees or whole rotations per second, you should multiply it by Time.deltaTime (the amount of time that has passed since the previous Update) to convert it to rotation per update frame.

Multiplying DiagonalWalkRotationSpeed by the delta time should make your thing rotate more sensibly.

Comment
Add comment · Show 1 · 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 string_username · Mar 05, 2017 at 07:07 PM 0
Share

Well I DID forget multiplying with Time.deltaTime so I appreciate that, but that doesn't seem to be the problem as the euler angle still doesn't even change at all until diagDeg$$anonymous$$oved > diagRot.

avatar image
0

Answer by UnityCoach · Mar 06, 2017 at 08:44 AM

If I'm not mistaken, eulerAngles is a Vector3. Vector3 is a struct, not a class. So, when you do something like

 transform.eulerAngles *= Vector3.up * 90;

it doesn't update the transform.eulerAngles. You need to assign it back.

 transform.eulerAngles = transform.eulerAngles.Scale (Vector3.up * 90);
Comment
Add comment · 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

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

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

How to flip euler angle without flipping axis 0 Answers

How do I modify animation parameters from script (C#) 1 Answer

Play animation once (when down key pressed) in c# 1 Answer

Animation/Movement Error 1 Answer

Looking to activate animation when clicking a button 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