Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Daniel77 · Aug 14, 2011 at 02:26 PM · c#javascriptraycast

Raycasting C# to Unityscript Vector2 problem.

Hi all,

I am working on converting so C# code to Unityscript and have found a problem that i am having problem solving.

The C# is:

 activePiece.transform.position = PiecePosition((Vector2)piecePositions[activePiece.name]);

And as far as i have gotten in the conversion to unityscript is:

 activePiece.transform.position = PiecePosition(piecePositions[activePiece.name]);

I had to removed the Vector2 because it kept coming up with an error and would not let me test the game.

Without the Vector2 i can run the game, but as soon as i click on a puzzle piece i get the following error.

 InvalidCastException: Cannot cast from source type to destination type.
 JigsawPuzzle.Update () (at Assets/Scripts/JigsawPuzzle.js:324)
 DemoJigsawPuzzle.Update () (at Assets/Scripts/DemoJigsawPuzzle.js:41)


The error is being called on the line 324 which is the lie of code above that i (tried to) convert, problem is i am not sure how to format the code so that i can make it work with the Vector2 all combinations that i have tried have led to errors.

A small breakdown of the code for you.

activePiece is a gameobject. PiecePosition is a function. piecePositions is a Hashtable.

Any help would be greatly appreciated. :)

Thanks.

-EDIT-

C#:

 // calculate right position for a x,y positioned piece on puzzle 
     private Vector3 PiecePosition(Vector3 pos)
     {
         float dX = transform.localScale.x / size.x;
         float dY = transform.localScale.y / size.y;
 
         // determine the position related x/y vector for this piece
         Vector3 positionVector =
             ((((transform.localScale.x / 2) * -1) + (dX * (pos.x - 1))+ (dX * (spacing/2))) * transform.right * -1) +
             (((transform.localScale.y / 2)) - (dY * (pos.y - 1)) - (dY * (spacing/2))) * transform.up;
 
         // set piece position to its right spot on the puzzle
         return transform.position +
             transform.forward * ((transform.localScale.z / 2) + 0.001f) +
             positionVector;
     }


And what i converted it to in unityscript:

 //Calculate right position for a x.y positioned piece on puzzle.
     function PiecePosition(pos : Vector3) : Vector3
     {
         var dX : float = transform.localScale.x / size.x;
         var dY : float = transform.localScale.y / size.y;
         
         //Determine the position related x/y vector for this piece.
         var positionVector : Vector3 = ((((transform.localScale.x / 2) * -1) + (dX * (pos.x - 1)) + (dX * (spacing / 2))) * transform.right * -1) + (((transform.localScale.y / 2)) - (dY * (pos.y - 1)) - (dY * (spacing / 2))) * transform.up;
         
         //Set piece position to its right spot on the puzzle.
         return transform.position + transform.forward * ((transform.localScale.z / 2) + 0.001) + positionVector;
     }


-EDIT2-

Ohh wait you wanted piecePositions.

This is the Hashtable.

 private var piecePositions = new Hashtable();
Comment
Add comment · Show 1
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 aldonaletto · Aug 14, 2011 at 04:28 PM 0
Share

How piecePositions was declared? From an older question of yours, PiecePosition expects a Vector3 argument and returns Vector3, what matches transform.position, thus the problem is with the Hashtable. Add to your question the line where piecePositions is declared.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Waz · Aug 14, 2011 at 02:28 PM

At the top of your file, put:

 #pragma strict

then the compiler will tell you what is wrong, instead of leaving until runtime to give you a nasty surprise. Yes, this should be the default.

In this case, you're actually going to run into more trouble, since you have a Hashtable of a ValueType.

Best to just convert that to a Dictionary.<String,Vector2>, and benefit from better error messages and better performance.

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 Daniel77 · Aug 14, 2011 at 03:18 PM 0
Share

I put #pragma strict at the top and ran the game and got the following error.

$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.BoxCollider.Raycast'. Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.ProduceExtensionDispatcher () Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.Create () Boo.Lang.Runtime.RuntimeServices.DoCreate$$anonymous$$ethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices.Create$$anonymous$$ethodDispatcher (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey13.<>m_7 () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.Dispatcher$$anonymous$$ey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cache$$anonymous$$eyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) JigsawPuzzle.Update () (at Assets/Scripts/JigsawPuzzle.js:274) DemoJigsawPuzzle.Update () (at Assets/Scripts/DemoJigsawPuzzle.js:41)

I got no idea what all that means but it directed me to line 274 which is.

if (puzzlePlane.collider.Raycast(Camera.main.ScreenPointToRay(position), hit,Vector3.Distance(Camera.main.transform.position, transform.position)*2))

I am going to look up and see how Dictionary. , works

avatar image Waz · Aug 14, 2011 at 03:59 PM 0
Share

So this means you have other coding errors that you haven't noticed yet. This Raycast one looks particularly nasty, I can't imagine what the non-strict (a.k.a. anarchist) compiler is doing with your code there! You can't call Raycast on a Collider, it is a class function of Physics! Just work through them one-by-one, the compiler is finding bugs for you.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Physics.Raycast to Physics2D.Raycast? 1 Answer

C# raycast shooting script broken 1 Answer

How to Raycast from Camera View Point? 2 Answers


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