- Home /
How do I set an objects euler rotations to match the objects transform in C#?
I have a script that is almost functional, it gives some good output as to where the player is standing relative to where an object is facing...this could be used to adjust a texture offset with some billboarding to recreate those good ol' original Doom style enemies.
My problem is that the euler transforms are constant and have nothing to do with an object's rotations or transform. I can debug my game with an object facing me and the console says he is facing me...I rotate the object by 180 and it still says he is facing me.
Is it possible to 'zero out' the object's eulers to match the objects transform in the update in C#?
Also would those adjusted euler angles be set for only that object or would this adjust the calculations for every code using eulers in the game?...basically a catastrophe...
We'd need to see some code to help. I would strongly suggest that you embrace Quaternions though. Working with Euler angles breaks down quickly if you have object hierarchies deeper than two levels.
the Top calculates the horizontal calculations and the lower the vertical...I get some great output into my console but it never adjusts based on which way the object faces.
using UnityEngine;
using System.Collections;
public class facedebug : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var dir = Camera.current.transform.position - transform.position;
var angle = ($$anonymous$$athf.Atan2(dir.z, dir.x) + $$anonymous$$athf.PI) * (180 / $$anonymous$$athf.PI); // range 0 to 360
var horizatanindex = $$anonymous$$athf.RoundToInt(angle / 45) % 8; // range 0 to 7
//Debug.Log (horizatanindex);
var dir2 = Camera.current.transform.position - transform.position;
var angle2 = ($$anonymous$$athf.Atan2(dir2.y, dir2.z) + $$anonymous$$athf.PI) * (180 / $$anonymous$$athf.PI); // range 0 to 360
var vertatanindex = $$anonymous$$athf.RoundToInt(angle2 / 45) % 8; // range 0 to 7
Debug.Log (vertatanindex);
}
}
...also the var's are aligned...my copy paste seems to have messed up the placement for the above post
To explain a bit more...this gives out a value between 0 and 7 based on where the camera is (not the player as I mistakenly posted before) in relation to where the object is facing...i.e. is the camera looking at the objects right-side, left-side, it's back, face etc. The vertical gives out put if youa re looking at the top of an objects head, the bottom of it's feet etc.
At least it should...The output I get seems correct but completely ignores where the object is actually facing and seems fixed somehow.
Answer by wibble82 · Apr 30, 2014 at 05:32 PM
Hi there
The object's euler values are its rotation. Basically the transform stores the local position as a vector3, the local scale as a vector3 and the local rotation as a quaternion. Internally these are combined with the parent, and its parent and its parent etc etc to get its world position/scale/rotation.
The euler angles member on transform is a property (effectively a special function that doesn't need parameters) that:
when setting the euler angles, converts them to a quaternion then sets the rotation to the new value
when reading the euler angles, converts the rotation to euler angles then returns them
If you aren't seeing the euler angles change then there's something else odd happening. I would guess that somehow you're looking at the euler angles of the wrong object. If you can see your object is rotating then the euler angles should be changing. However, if its a child of an object that's rotating, its local rotation won't be (which is the one that is displayed in the transform in the property inspector).
Your answer
Follow this Question
Related Questions
How to go from eulerAngle axis 270 to 0 by rotating 90 degrees? 1 Answer
I can't set the rotation of an object, either with scripts or directly in the editor. 1 Answer
How to use an axis of a eulerAngle for a targetAngle on a HingeJoint? [C#] 0 Answers
Distribute terrain in zones 3 Answers
Particle System wont play? 1 Answer