- Home /
Saving Object Position in Float NOT Object
Hey,
So I'm new to Unity, as you can probably guess by the question I'm also new to Javascript. I'm finding this whole non static variable declaration lark to be more of a curse than a blessing. I'm trying to grab a sprite's position via x and y coordinates (2D) and use them in a calculation for velocity. However Unity tells me I cannot perform said operation on two "variables" of type object. Which is fair enough. I can't seem to figure out how to typecast these objects into float type variables, in Java it would have been simple enough but here, in javascript, I'm completely and utterly baffled.
Here's what I'm using to store the position values:
characterPositionX = character.position.x;
characterPositionY = character.position.y;
The variables declared like so:
var characterPositionX;
var characterPositionY;
I've tried using ": float" however I'm pretty sure that's purely to do with MonoBehaviour and not type casting(?). I'm honestly lost here, if anyone can offer up some help it'd be greatly appreciated.
Cheers, Lewis
You're wrong - the : float
bit is important :) But are you sure you need to separate out the x and y components? You should be able to calculate velocity working purely with Vector2 inbuilt functions (hard to say without seeing more of your code)
Answer by Xumbu · May 24, 2014 at 12:07 PM
with java background, why don't you use csharp?
I was considering it, and started using it but decided I would try a different language. I'm doing this for educational purposes rather than entertainment, so figured I'd learn a language that's different in structure to Java a little more than C# is.
Your answer
Follow this Question
Related Questions
Moving an object 0 Answers
Why Does My Object Change Position During Update? 0 Answers
How to move an object from one position to desired position? 0 Answers
'enabled' is not a member of 'Object' 1 Answer
2D get touch input 1 Answer