- Home /
Vector3 being treated as a variable
So I am trying to code my first ever game in Unity3d (C#). I want to initialise my player game object (just a cube primitive) at the co-ordinates of 0,0,0. I wrote this in the start() function:
transform.position = Vector3(0, 0, 0);
I recognise that I am using a struct as if it were a variable, however I can't think of another way to do it! This is also, confusingly, the example shown within the scripting reference. Any help would be greatly appreciated, even if you don't want to tell me flat out, just pointing me in the right direction would be awesome.
Thanks.
Answer by whydoidoit · Mar 14, 2014 at 03:53 PM
I guess you are using C#? In C# it is:
transform.position = new Vector3(0,0,0);
You can also use standard predefined vectors directly such as Vector3.zero or Vector3.one;
transform.position = Vector3.zero;
Or to put the item at -100,0,0 you could do:
transform.position = Vector3.right * -100;
Ah, so do you $$anonymous$$d if I guess the logic behind that? Is it because new creates a new instance of vector 3? Why do I need to use new? By the way, thank you for responding :)
Please post comments using the Add New Comment, don't use Answer which is for solutions only. I have converted it for you.
You are right, you are creating a vector3 out of the parameters 0,0,0. When you create something in C# you use the new keyword always (it is optional in Unity Script). The only time it is not necessary is when the type is primitive like a float or an int.
You can also use standard Vectors for this stuff - as these already exist you don't need new:
transform.position = Vector3.zero;
Of course if you have your position in a variable that also is just assigned:
transform.position = somePosition;
If you are declaring the 0's it is good practice to use transform.position = new Vector3(0f,0f,0f);
@putlucky - could you tick my answer if you feel it's covered your issue. It will get us both some $$anonymous$$arma...
@whydoidoit You should probably edit it to include the Vector3.zero comment so others who come across this don't have to read all the comments for the full answer.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Beginner Help with Unity 1 Answer
A node in a childnode? 1 Answer
Fps Controller movement is broken 0 Answers
What is the best way to learn Unity3D? 6 Answers