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 Borzi · Sep 29, 2013 at 11:30 AM · cameratransformpositionvector3transform.position

Trouble converting transform.position to C#

I am trying to get transform.position (so the objects current position/rotation) to work in C#. I have created a temporary variable to store the information in but it doesn't really seem to work...anybody know how to help me? Thank you in advance!

 void Update () 
     {
     Vector3 MyTransform = transform.position;
     MyTransform.position = PlayerCamera.transform.position + (Quaternion.Euler(0, targetYrotation,0) * Vector3(holdingSide, holdingHeight, 0));
     }
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
Best Answer

Answer by hamstar · Sep 29, 2013 at 11:54 AM

Your variable MyTransfrom already points to transform.position, so writing MyTransform.position is like writing transform.position.position.

Do you mean to have this?

 Transform myTransform = transform;
 myTransform.position = PlayerCamera.transform.position + (Quaternion.Euler(0, targetYrotation,0) * new Vector3(holdingSide, holdingHeight, 0));

Note that you need the keyword new before the Vector3 constructor as you are creating a new Vector3 object. Appologies if I have made a mistake, I don't have monodevelop on this PC.

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 Borzi · Sep 29, 2013 at 12:37 PM 0
Share
 myTransform = transform;
 myTransform.position = PlayerCamera.transform.position + (Quaternion.Euler(0, targetYrotation,0) * Vector3(holdingSide, holdingHeight, 0));


Thanks for your reply, I rewrote the code, but I still get one error: "Expression denotes a type', where a variable', value' or method group' was expected". I don't know what this means & could you help me?

avatar image hamstar · Sep 29, 2013 at 02:12 PM 0
Share

The multiplication expects a vector3 object so you need the word new as I mentioned.

avatar image Hoeloe · Sep 29, 2013 at 03:37 PM 1
Share

To elaborate - C# requires you to be much more explicit than UnityScript. In other languages, when you write something like Vector3(1,2,0), it implicitly creates a new object and stores it in memory. C# doesn't allow this - it is much stricter, and requires that you explicitly tell it that you want to assign to memory. In order to do that, you use the new keyword, which tells the language "Yes, I want to create a new object, and assign it to memory so I can use it in my calculation". There are a lot of reasons things like this are done in C#, which I won't go into, but on the whole they lead to a much stronger, more powerful language.

avatar image Borzi · Sep 29, 2013 at 06:30 PM 0
Share

Ehm...thanks for the explanation but this did not really help me because I still have no idea of how to solve this problem.

avatar image hamstar · Sep 29, 2013 at 06:37 PM 1
Share

This is after you added new? I just checked the code with Unity and only get that error if I remove "new". Are you sure the error is from this line of code?

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

17 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

Related Questions

Help with this script? Keeps moving my objects around in run-time? 0 Answers

Move camera when mouse is near the edges of the screen 1 Answer

Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer

collider triggers transform position 1 Answer

Any help with prevent camera from passing a specific coordinate? 1 Answer


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