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 PaxStyle · Jun 22, 2014 at 04:29 PM · transformposition

transform.position question

Hi guys. :) my question is: how can I do for move a transverse object Up and Down with transform position? i posted a pic. This way is wrong? It's convenient make the object straight and after change its position?

alt text

 #pragma strict
 
 public var GameObject : Pistone;

 function Update () {
 
 if(Input.GetKeyDown(KeyCode.A)) {
 Pistone.transform.position = Vector3(0,0,0);
 }
 
 }


1-horz.jpg (158.0 kB)
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 robertbu · Jun 22, 2014 at 04:54 PM

Assuming the object is a child of the rod, and that you aligned the two before the whole thing is rotated, you can just use Transform.localPosition for the movement. Alternately, you can use Transform.Translate(). Translate() uses local coordinates by default.

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 PaxStyle · Jun 23, 2014 at 09:19 AM 0
Share

Ok, but the piston dosn't move Up and Down, but transversely.

 var move : float = 0.1;
 var translation : float;
 
 function Start () {
 
 }
 
 function Update () {
 
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
 
 translation = Time.deltaTime * move;
 transform.Translate(new Vector3(0,1,0) * translation);
 
 }
 
 
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) {
 
 translation = Time.deltaTime * move;
 transform.Translate(new Vector3(0,-1,0) * translation);
 
 }
 
 
 }

alt text

21.jpg (8.1 kB)
avatar image robertbu · Jun 24, 2014 at 02:12 PM 0
Share

You are moving it on the local 'Y':

  transform.Translate(new Vector3(0,1,0) * translation);

This means that your rotation is not what you expect. It could be for several reasons, but the most likely is that you constructed your child horizontally and have it rotated with respect to the parent. So you can fix it in a couple of different ways. First, just pick the local axis that does move it up and down the rod. It will be 'x' or 'z'. Or you can go back and rotate your object in your modeling program. If you are looking for easy 'y' movement, you want both the rod and the object to be vertical when their rotations are (0,0,0).

avatar image PaxStyle · Jun 24, 2014 at 02:31 PM 0
Share

When I move the piston Up with local axis (in the scene), in the transform change the 'Y' position, for this reason I set (0,1,0). And now? I have to straighten the piston, set the code when the piston is straight, and after incline it?

avatar image robertbu · Jun 24, 2014 at 02:45 PM 0
Share

I'm assu$$anonymous$$g your rod is a Unity Cylinder that is vertical when the rotation is (0,0,0). In order to move your cylinder on the local 'Y' axis, and have it move up and down the rod, your cylinder has to also be constructed so that when you import it from your modeling program, it is vertical when the rotation is (0,0,0). As an alternate to my two suggestions above, you can also solve the problem by making the cylinder a child of an empty game object.

avatar image PaxStyle · Jun 24, 2014 at 03:24 PM 0
Share

So I have to create a empty game-object, and put into it the piston?

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

2 People are following this question.

avatar image avatar image

Related Questions

Setting Position of Spawned Prefab 2 Answers

How do I make one transform rotation the same as anothers? 2 Answers

Objects with same position appear in different places 2 Answers

Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer

GameObject won't move to Transform Position 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