- Home /
Translate from JS to C# localEulerAngles
Hi i need help with some js code example i dont know how to use transform.localEulerAngles in C# properly.Basicly its a script for a character that turns the character to certain angles without turning the camera too. What i dont understand or find any examples how to declare var body : transform in C# to be usable like in JS. If instead i use: transform.localEulerAngles.y = 90; It says cannot modify value because its not a variable.Ive tried several examples i found with Vector3 and eulerAngles but they all turn the camera around too. Im beginner learning C# and unity,so i have rewrote in C# what i found in this nice little tutorial http://www.youtube.com/watch?v=7hXSbA4sr-Q
var body : Transform;
function LateUpdate ()
{
if(Input.GetAxis("Vertical") == 0)
{
if(Input.GetAxis("Horizontal") > 0)
{
body.localEulerAngles.y = 90;
}
else if(Input.GetAxis("Horizontal") < 0)
{
body.localEulerAngles.y = -90;
}
}
..... Thanks for any help.
Edit: essentially what he rotates in the video is the biped of the character by writing var body:transform;(29:30 in video tut) then he can drag the biped in the new slot (transform). I need how to i do that in C# if i write var body = Transform; i get erors obviously. Sory i wasnt being explicit what i need.
Answer by robertbu · Mar 01, 2014 at 05:54 PM
First no matter what the language, it is best not to set a axis independently. From the reference on Transform.eulerAngles:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once...
While it is a bit of a hastle at times, the trick to doing this in C# is to copy out the values to a Vector3, change whatever you need to change, and reassign the values (and you should be doing that for both Transform.eulerAngles and Transform.localEulerAngles in JS as well):
Vector3 v = body.localEulerAngles;
v.y = 90;
body.localEulerAngles = v;
I dont think i understand sorry for being a total noob. What im trying to achieve is like in the video at 28:24 he declares body and drags the biped to it it unity,i already have an nimated character with run/idle animations set up, so when i press S the character will face the camera while running in that direction. I dont know how to do that in C#
I don't know about your specific code doing what you want it to do. What I'm addressing the the compiler error you get when you attempt to assign to one axis in eulerAngles in C#. I'm addressing how you convert the above Javascript to C#.
So for example looking at lines 7 - 10 of your Javascript script, they would be rewritten in C# as:
if(Input.GetAxis("Horizontal") > 0)
{
Vector3 v = body.localEulerAngles;
v.y = 90.0f;
body.localEulerAngles v;
}
This will now compile and do what your Javascript was doing (without any of the bugs that directly assigning an axis independently would have cased in either language). And this should have been what you were doing in Javascript as well when using eulerAngles and localEulerAngles.
Now i understand what you wrote. But my main problem is "body" which needs to be declared first or so. Im trying to figure out the basics, the first problem "var body : Transform" thats what i dont know how to write in C#,in JS a new slot appears in unity at js script and he drags the biped into the body(transform),thats what i try to achieve. If you could watch the video at 29:30 is where the script is actually from.
That becomes in C#:
public Transform body;
Variables in Javascript are public by default. In C# you have to declare them public. Here is a reference for the syntax differences between Javascript and C#:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
That solved my problem. It was quite simple but i couldnt see it.And i think i asked the wrong question to begin with $$anonymous$$any thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Character Transform 3 Answers
Maintaining direction while jumping / rotating 3 Answers
C# Mathf.PingPong Rotate Back and Forth 2 Answers
Wheels Rotate or Turn, but not both 1 Answer