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 Deadmarine · Jun 30, 2014 at 09:35 PM · c#javascript

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

Comment
Add comment · Show 6
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 iwaldrop · Jun 30, 2014 at 10:09 PM 2
Share

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);

avatar image iwaldrop · Jun 30, 2014 at 10:12 PM 2
Share

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;
 }

avatar image Bunny83 · Jul 01, 2014 at 12:30 AM 1
Share

@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.

avatar image iwaldrop · Jul 01, 2014 at 12:50 AM 0
Share

@bunny83 Thanks for the clarification!

avatar image Deadmarine · Jul 01, 2014 at 01:39 AM 0
Share

Case anyone is curious coll.bounds.size.y doesn't work. returns BCE0019: 'bounds' is not a member of 'UnityEngine.Collision2D'.

Show more comments

1 Reply

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

Answer by Kiwasi · Jun 30, 2014 at 11:59 PM

Generic answer for 'help me convert this script'

Several methods work. In preferred order

  1. Understand what the script does in the first place, and why it works. Then write it from scratch in the new language

  2. Use the documentation. There is a button that lets you pick between syntax and examples in JavaScript and C#

  3. 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.

  4. 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;


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 Deadmarine · Jul 01, 2014 at 01:39 AM 0
Share

Thank you for your answer worked like a charm.

avatar image Kiwasi · Jul 01, 2014 at 01:46 AM 0
Share

You went for option 1? :)

Glad I could be of help.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

I need help knowing if Im doin this Right 1 Answer


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