Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Nubz · Dec 19, 2012 at 03:55 AM · c#convert

Trying to Convert this JS to C#

I have been trying to convert a script in a book to c# since that is the langauge I am most comfortable with and can't seem to figure how to make it work

these are the variables I need at the top it says

 var rotationSpeed : float = 20.0; 
 private var rotationSensitivity = 0.1 ;

and this is the FixedUpdate in JS

 function FixedUpdate ()
 { 
 if(Input.GetAxis("Turn"))
 {
 var rotation : float = ( Input.GetAxis("Turn") ) * rotationSpeed * rotationSensitivity; 
 rotation = rotation + transform.eulerAngles.y ; 
 transform.localRotation = Quaternion.AngleAxis ( rotation, Vector3.up ) ; 
 }

in C# I get the problem with it saying "can not convert type float to bool" which makes sense I guess

Mostly need to be pointed in the right direction to fix it Or if you are feeling the season make my life easier and show me lol

P.S. I tried to format this so the code looked right and could not figure out how

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Shrandis · Dec 21, 2012 at 09:39 AM

This is the C# version of the code you posted:

     public float rotationSpeed = 20.0f;
     float rotationSensitivity = 0.1f;
 
     void FixedUpdate()
     {
         if (Input.GetAxis("Turn") != 0.0f)
         {
             float rotation = Input.GetAxis("Turn") * rotationSpeed * rotationSensitivity;
             rotation = rotation + transform.eulerAngles.y;
             transform.localRotation = Quaternion.AngleAxis(rotation, Vector3.up);
         }
     }

It is a bit prettier like this:

     public float rotationSpeed = 20.0f;
     private float rotationSensitivity = 0.1f;
 
     void FixedUpdate()
     {
         float turnAxis = Input.GetAxis("Turn");
         if (turnAxis != 0.0f)
         {
             float rotation = turnAxis * rotationSpeed * rotationSensitivity + transform.eulerAngles.y;            
             transform.localRotation = Quaternion.AngleAxis(rotation, Vector3.up);
         }
     }

Please don't forget to accept the answer which solved your problem.

Comment
Add comment · Show 4 · 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 Nubz · Dec 21, 2012 at 12:49 PM 0
Share

I would if I got an answer that actually worked LOL(meant to be funny both of you have helped me figure out pieces of it and I am thankful) anyway yours and the one that I came up with after I posted work but the player keeps turning and doesn't stop when you let go of the button

avatar image Shrandis · Dec 21, 2012 at 01:05 PM 1
Share

Go to Edit > Project Settings > Input.

Find your axis "Turn" and make sure its Gravity value is set to something other than 0. A negative gravity value causes acceleration, positive gravity causes deceleration.

avatar image aldonaletto · Dec 21, 2012 at 04:09 PM 0
Share

Yes: the property gravity in the Input $$anonymous$$anager tells how fast the axis returns to zero when you release the button. If it's zero, GetAxis returns the last value forever.

avatar image Nubz · Dec 22, 2012 at 02:49 AM 0
Share

Ahhh That was the next thing I was going to look into And it makes perfect sense Works just like I wanted now Thank You to everyone for your help it is very much appreciated

avatar image
1

Answer by aldonaletto · Dec 19, 2012 at 04:39 AM

This error seems to come from:

 if(Input.GetAxis("Turn"))

GetAxis returns a float, and the if requires a boolean expression. You could replace this with:

 if (Input.GetAxis("Turn") != 0.0f)
Comment
Add comment · Show 2 · 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 Nubz · Dec 19, 2012 at 07:53 AM 0
Share

I'll give that a try I did get it working to some point by taking out the if

void FixedUpdate() { Input.GetAxis("Turn"); var rotation = Input.GetAxis("Turn") RotationSpeed RotationSensitivity ; rotation = rotation + transform.eulerAngles.y ; transform.localRotation = Quaternion.AngleAxis ( rotation, Vector3.up ) ; }

but then it just keeps turning and doesn't stop

avatar image Nubz · Dec 21, 2012 at 07:58 AM 0
Share

didn't work

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

11 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

Related Questions

Js to C# convert 1 Answer

Converting JS to C# 3 Answers

Lightning Script Js To C# Conversion errors 2 Answers

Multiple Cars not working 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