- Home /
Help converting js to C# from Brackey's tutorial.
I've been watching the Brackey's pong tutorials on youTube and I have been trying to convert his javascript to C#. I've done excellent up until this portion. Any help is appreciated. Video Link: Brackey's Pong Tutorial Link
void Update () {
var xVel : float = rigidbody2D.velocity.x;
if (xVel < 18 && xVel > -18 && xVel != 0) {
if (xVel > 0) {
rigidbody2D.velocity.x = 20;
}
else {
rigidbody2D.velocity.x = -20;
}
}
}
I tried changing
float xVel = rigidbody2D.velocity.x;
before posting, thinking that was my problem. but
{ rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y); }
was where I was having my problems. I wasn't thinking that I had to define a rigidbody2D.velocity.y.
Thank you a ton.
Can you please accept the answer below as correct just to close this Question, thanks.
Answer by mattyman174 · May 18, 2014 at 02:55 AM
void Update()
{
float xVel = rigidbody2D.velocity.x;
if (xVel < 18 && xVel > -18 && xVel != 0)
{
if (xVel > 0)
{
rigidbody2D.velocity = new Vector2(20, rigidbody2D.velocity.y);
}
else
{
rigidbody2D.velocity = new Vector2(-20, rigidbody2D.velocity.y);
}
}
}
That what your after?
Answer by rgowen · May 18, 2014 at 03:07 AM
The only line you should have to change is this one:
var xVel : float = rigidbody2D.velocity.x;
As C# is a strongly typed language, you just use the format type variableName = value
, like this:
float xVel = rigidbody2D.velocity.x;
I'd really suggest that you learn how to translate back and forth, especially if you're going to follow along with tutorials. A little goes a long way with that, as there seems to be a fairly equal distribution of people who use C# or JavaScript. If anything, it seems there are more people that use JavaScript.
Your answer
Follow this Question
Related Questions
Referencing variables / functions on another script 1 Answer
Block programming for Kid 1 Answer
Which programs should i use for programming a game? 1 Answer
How to find Gameobject with given coordinates 1 Answer
Rotate function on c# 1 Answer