Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xSpectrum · Sep 16, 2011 at 10:12 PM · c#positiony

How to Maintain Y Position? C#

I want to have the player press the "MouseButton3" and have whatever height he was at the time he pressed the button be his locked Y axis position until he presses the button again.

Ex: Player presses button when his Y position was at 10. His Y position is locked at 10 until he presses the button again/releases the button.

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
1

Answer by aldonaletto · Sep 16, 2011 at 10:54 PM

You must use a flag to indicate if it's locked or not. When the flag becomes true, save the locked height. If flag is true, force the y coordinate after the movement to the saved height:
EDITED: This answer had one error: the y element of transform.position was being assigned alone. It's valid in Unityscript, but not in C#, as @Bunny83 said - the whole structure must be assigned. This error fixed now.

float lockedY; // holds the locked height bool lock = false; // true if locked

void Update(){ if (Input.GetButtonDown("MouseButton3")){ // I suppose that's your button name lock = !lock; // toggle lock flag if (lock) lockedY = transform.position.y; // if lock became true save locked height } // do your movement stuff here // then force y to the locked height if lock active if (lock){ Vector3 tmp = transform.position; // position is actually a tmp.y = lockedY; // property, thus you must set transform.position = tmp; // all their members at once } }

Comment
Add comment · Show 2 · 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 Bunny83 · Sep 17, 2011 at 02:22 AM 0
Share

Don't forget that you can't set a member of a struct-property. You have to assign the whole Vector3 in C#.

 if (lock)
 {
     Vector3 tmp = transform.position;
     tmp.y = lockedY;
     transform.position = tmp;
 }
avatar image aldonaletto · Sep 17, 2011 at 12:19 PM 0
Share

That's what happens when a JS guy tries to write C#... Thanks, @Bunny83!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help! How to change Real numbers to Natural numbers on Distance Counter script (Noob question) 0 Answers

addition of vector3 1 Answer

Positioning a GameObject so I't wont be inside another object 1 Answer

Distribute terrain in zones 3 Answers

Checki f Player is within area? C# 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