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,
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.
$$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.
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.
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.
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220612112516im_/https://answers.unity.com/themes/thub/images/avi.jpg)