- Home /
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
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.
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
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.
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.
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
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)
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
Your answer
Follow this Question
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