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 Bokaii · Nov 19, 2014 at 08:23 PM · multiplayerfpsonlineparkour

Add one on local z axis

I wan't to add one to my players local z axis... Like:

 void Start(){
     Vector3 updatedPos = transform.localPosition;
     updatesPos.z += 1;
     transform.position = updatedPos;
 
 }


This adds one on the z axis for the whole world (Can't remember what its called... opposite of local)

Comment
Add comment · Show 4
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 MrSoad · Nov 19, 2014 at 08:20 PM 0
Share

This might work :

 void Start() {
     transform.localPosition.x = transform.localPosition.x + 1;
 }
avatar image Bokaii · Nov 19, 2014 at 08:28 PM 0
Share

It says I can't change it, and I need to consider storing it in a variable.

avatar image MrSoad · Nov 19, 2014 at 10:43 PM 0
Share

Yep I was not sure, js would let you do this but I know C# is far more strict about how you have to do things, sorry for the bad info :(

avatar image Bokaii · Nov 20, 2014 at 06:48 AM 0
Share

Its cool :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Uldeim · Nov 19, 2014 at 10:28 PM

Vector3s are immutable; you can't actually change their values. This is due to how they're implemented (as structs; ie. Value Types).

Try the following:

   //Shorten the name
   var locPos = transform.localPosition;
   Vector3 updatedPos = new Vector3(locPos.x, locPos.y, locPos.z + 1);

EDIT:

Oops, the question was actually how to move along the object's z-axis, not to move it in local space.

The solution was to use the transform.Translate() method.

Comment
Add comment · Show 7 · 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 Bokaii · Nov 20, 2014 at 06:47 AM 0
Share

Doesn't work... :(

avatar image Uldeim · Nov 20, 2014 at 06:50 AM 0
Share

Uh, could you be more specific?

avatar image Bokaii · Nov 20, 2014 at 06:51 AM 0
Share

It moves on the z axis, but not the local z axis.

avatar image Uldeim · Nov 20, 2014 at 06:54 AM 0
Share

Just so we're clear, your code currently looks like this?

 void Start(){
      var locPos = transform.localPosition;
      Vector3 updatedPos = new Vector3(locPos.x, locPos.y, locPos.z + 1);
      transform.localPosition = updatedPos; 
  }

As other people have mentioned, the last line definitely needs to be localPosition and not position.

avatar image Bokaii · Nov 20, 2014 at 07:11 AM 0
Share

Yes thats what it looks like.

Show more comments
avatar image
0

Answer by Jeff-Kesselman · Nov 19, 2014 at 08:25 PM

What is the difference between this line...

 Vector3 updatedPos = transform.localPosition;

and this line?

 transform.position = updatedPos;

Answer that question and you will know what is wrong with your code.

Comment
Add comment · Show 1 · 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 Bokaii · Nov 19, 2014 at 08:27 PM 0
Share

If its:

Vector3 updatedPos = transform.localPosition; transform.localPosition = updatedPos;

I already tried that. And it doesn't work! :(

avatar image
0

Answer by hsgaminators · Aug 30, 2021 at 06:30 PM

transform.localPosition += new Vector3(0f,0f,1f);

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

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

6 People are following this question.

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

Related Questions

yield WaitForSeconds c# 4 Answers

Find closest object with tag 1 Answer

Bullet Effect (RaycastAll Question) 1 Answer

Find children of object and store in an array 3 Answers

unity3d invoke 0 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