Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ionut Hurjui · Mar 01, 2014 at 05:52 PM · c#transformcharacterlocaleulerangles

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.

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
1
Best Answer

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;
Comment
Add comment · Show 5 · 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 Ionut Hurjui · Mar 01, 2014 at 06:30 PM 0
Share

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#

avatar image robertbu · Mar 01, 2014 at 06:45 PM 0
Share

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.

avatar image Ionut Hurjui · Mar 01, 2014 at 07:04 PM 0
Share

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.

avatar image robertbu · Mar 01, 2014 at 07:08 PM 0
Share

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

avatar image Ionut Hurjui · Mar 01, 2014 at 07:14 PM 0
Share

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

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

20 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

Related Questions

Maintaining direction while jumping / rotating 3 Answers

C# Mathf.PingPong Rotate Back and Forth 2 Answers

js to C# - Transform 1 Answer

Wheels Rotate or Turn, but not both 1 Answer

Distribute terrain in zones 3 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