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 Sup · Nov 26, 2013 at 02:39 PM · axismove an object

Moving an object a specific amount of units.

When I click on a game object, I'd like it to move a specific amount of units on my X axis. For Example, I have a cylinder, that when clicked on moves exactly 20 units on my X axis.

I don't want it to move to another object, just 20 units in a certain direction. If anyone could help me with this problem I'd be forever grateful.

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
0

Answer by thaiscorpion · Nov 26, 2013 at 02:48 PM

When you detect the click just change the x position:

 transform.position.x += 20;

This will change the position on the x axis 20 units, just make sure 'transform' is the transform of the object you want to move.

Comment
Add comment · Show 3 · 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 Sup · Nov 27, 2013 at 11:00 AM 0
Share

Thanks for your answer.

When I put that in I get this error: Assets/Scripts/Navigation.cs(35,35): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

I'm 100% clueless on what this means.

I'd reckon when I get this to work, the object would instantly blink to that position. I'd like to slide it 20 units with a certain amount of speed. Any ideas?

Thanks.

avatar image thaiscorpion · Nov 27, 2013 at 11:04 AM 0
Share

well I dont use C I'm more of a javascript guy :D I've never seen that error before.

For the sliding effect you could use a lerp, check this page for reference:

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Lerp.html

avatar image robertbu · Nov 27, 2013 at 11:33 AM 0
Share

In C#, you have to do it this way:

 Vector3 v3 = transform.position;
 v3.x += 20.0f;
 transform.position = v3;

Or you can do it this way:

 transform.position += Vector3.right * 20.0f;

Or:

 transform.Translate(20.0f,0.0f, 0.0f, Space.World);

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

How do i make a cube "slide"and fixed on a rail? 0 Answers

limiting rotation to solid angle 5 Answers

Rotatearound Collider axis 1 Answer

,Switching 2D platformer from x.y to z,y 0 Answers

Limit Speed One Axis 2 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