I have been following the tutorial at noobtuts - 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