- Home /
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();
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.
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.
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
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
![](https://koobas.hobune.stream/wayback/20220612180433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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