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 Dudledok · Nov 09, 2013 at 01:40 PM · c#movementrigidbodyendless runnerrigidbody.velocity

Multi-lane endless runner. How to toggle lanes?

I have a character which is moving constantly along the x axis, and you can jump altering the y axis velocity, and I don't want the character to be able to move on the z axis, unless you jump left or right.

Everything is working, except when jumping left or right where the character will continue to slide in that direction after landing, and I thought the following would solve this, but there is no noticeable effect.

 //when touching a platform i.e. landing the jump
 float zVel = rigidbody.velocity.z;
 zVel = 0f;

I believe instead of making the character jump using rigidbody.AddForce(... I should just make the character move in the z axis by a certain distance. I was hoping something along the lines of the following would be possible:

 rigidbody.transform.position += (0,0,5);

What is the right way to do this?

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
Best Answer

Answer by fafase · Nov 09, 2013 at 01:51 PM

Ok here is what is happening:

 float zVel = rigidbody.velocity.z;
 zVel = 0f;

zVel gets a piece of paper and ask rigidbody to go down to velocity and get the value of z. Rigidbody does so and tells zVel the value there. zvel then writes it on his own paper.

Next line zVel scratches his paper and overwrite 0 on it and goes on with his life. rigidbody was not told about anyhting and z is still the value it was.

What you need is to tell rigidbody to change the value of the velocity:

 Vector3 vec = rigidbody.velocity;
 vec.z = 0;
 rigidbody.velocity = vec;

Here, vec gets the value of velocity on his paper, then overwrites his own value and then tells rigidbody to overwrite his value with the one from vec. Obviously only z has changed.

Please, do not get me wrong, I am not looking down on you using some kind of childish example, it just works on my students (university) to use some real world examples to explain what happens down in memory.

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 Dudledok · Nov 09, 2013 at 02:09 PM 0
Share

Thanks. I actually copied the code here wrong, but I was making a similar mistake.

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

16 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Unity Physics On Input Issue? 0 Answers

Movement Script Only Works in One Scene 2 Answers

Making RB movement smoother/curvy. C#. 1 Answer

isKinematic object doesnt collide with wall 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