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 putlucky · Mar 14, 2014 at 03:53 PM · beginner

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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;
Comment
Add comment · Show 6 · 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 putlucky · Mar 14, 2014 at 03:57 PM 0
Share

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

avatar image whydoidoit · Mar 14, 2014 at 04:00 PM 0
Share

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;
avatar image LoungeKatt · Mar 14, 2014 at 04:07 PM 0
Share

If you are declaring the 0's it is good practice to use transform.position = new Vector3(0f,0f,0f);

avatar image whydoidoit · Mar 14, 2014 at 07:56 PM 0
Share

@putlucky - could you tick my answer if you feel it's covered your issue. It will get us both some $$anonymous$$arma...

avatar image LoungeKatt · Mar 14, 2014 at 08:03 PM 0
Share

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

Show more comments

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

22 People are following this question.

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

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


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