- Home /
Help converting C# to Unityscript
I have been following the tutorial at http://noobtuts.com/unity/2d-pong-game to make a quick pong game. Instead of writing it in C# i have been converting it to javascript (unityscript) as i read/write the code. I am having trouble translating part of the code to unityscript. Hoping someone can help me.
void OnCollisionEnter2D(Collision2D col) {
// Hit the left Racket?
if (col.gameObject.name == "RacketLeft") {
// Calculate hit Factor
float y=hitFactor(transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
// Calculate direction, set length to 1
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
rigidbody2D.velocity = dir * speed;
}
// Hit the right Racket?
if (col.gameObject.name == "RacketRight") {
// Calculate hit Factor
float y=hitFactor(transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
// Calculate direction, set length to 1
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
rigidbody2D.velocity = dir * speed;
}
}
What i have.
#pragma strict
var speed : float = 2;
private var y : float;
private var dir : Vector2;
function Start () {
rigidbody2D.velocity = Vector2.one.normalized * speed;
}
function hitFactor(ballPos : Vector2, racketPos: Vector2, racketHeight : float) : float{
return (ballPos.y - racketPos.y) / racketHeight;
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.name == "RacketLeft") {
y = hitFactor(transform.position, coll.transform.position, 0.64);
dir = Vector2(1, y).normalized;
rigidbody2D.velocity = dir * speed;
}
if(coll.gameObject.name == "RacketRight") {
y = hitFactor(transform.position, coll.transform.position, 0.64);
dir = Vector2(-1, y).normalized;
rigidbody2D.velocity = dir * speed;
}
}
I tried hard coding the paddle height but the ball seems to move funny after that. The real issue is what does ((Boxcollider2D)col.collider).size.y)
convert to in Unityscript
The line you're asking about, ((BoxCollider2D)col.collider).size.y)
, just gets the y size of a Box Collider. According to the docs, all Collider2Ds have a bounds element, so you really shouldn't need to cast it to a BoxCollider2D. So, just try the following:
y = hitFactor(transform.position, coll.transform.position, coll.bounds.size.y);
Furthermore, you can make the function more DRY like this:
function OnCollisionEnter2D(coll: Collision2D)
{
var direction : float = 1;
if (coll.gameObject.name.Equals("RacketRight"))
direction = -1;
y = hitFactor(transform.position, coll.transform.position, coll.bounds.size.y);
dir = Vector2(direciton, y).normalized;
rigidbody2D.velocity = dir * speed;
}
@iwaldrop: Collider.bounds returns the AABB of the collider which is in worldspace while the BoxCollider2D size is in local space. When the collider isn't rotated they should be about the same size, but bounds isn't a general replacement for the size of the collider.
Also your more "dry" version is not a replacement for the original code. If you collide with something else than RacketLeft or RacketRight the original code does nothing while you flip the velocity regardless of what you're hitting. It's probably not relevant, but it's not a simplification of the original code, it does something else, similar but different.
Case anyone is curious coll.bounds.size.y doesn't work. returns BCE0019: 'bounds' is not a member of 'UnityEngine.Collision2D'.
Answer by Kiwasi · Jun 30, 2014 at 11:59 PM
Generic answer for 'help me convert this script'
Several methods work. In preferred order
Understand what the script does in the first place, and why it works. Then write it from scratch in the new language
Use the documentation. There is a button that lets you pick between syntax and examples in JavaScript and C#
Use your awesome googling skills that found you this script in the first place to find and download a script in the correct language. Cut and paste into your project.
Post your code on Unity Answers.
The key difference between the two languages is syntax. Typing is important as well, depending on how lazy your original JavaScript writer was. All the methods work exactly the same.
Incidentally casting in UnityScript is done using the 'as' keyword. Try
(col.collider as Boxcollider2D).size.y;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to use / simulate a using declaration in unityscript 1 Answer
Selecting and deselecting not working 0 Answers