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 /
  • Help Room /
avatar image
0
Question by Owlytsr · Jan 15, 2019 at 04:20 PM · rigidbodytransformvelocityjumpforward

Any way to make the Rigidbody keep it's last known velocity after a jump?

Hello everyone! I'm making a FPS game where my character moves at high speeds an can jump around, but there's an issue.

Th eplayer has a Rigidbody, a Capsule Collider and two scripts.

The issue is the following: When i jump, i want the velocity of my Rigidbody to stay the same even if i look around. Do you think i'll be forced to put the camera outside of my prefab or is there a solution that would allow me to get the last known velocity at the moment of the jump or something / override the transform.forward velocity while in air and replacing it by another ( i'm a total newbie with code so maybe i'm not seeing something very obvious here)?

Here are my scripts (made using Brackey's tutorial on the online multiplayer fps) :

Script 1 (attached to the player prefab which hosts all the colliders and scripts :

link text

And here's the second, which executes all commands from the first and is automatically attached to the prefab :

link text

rbn-playercontrols.txt (3.0 kB)
rbn-playermovement.txt (2.6 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
2

Answer by tormentoarmagedoom · Jan 15, 2019 at 04:39 PM

Good day.

You can just store the velocity in a Vector3 (or 2) variable the moment of the jump, and apply it once it touch the groud again.

Your question is really very simple, you should take a look at some other tutorials about movement and rigidbody, I'm sure you will get in very soon by your own!

Good luck! Bye!

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 Owlytsr · Jan 16, 2019 at 11:00 AM 0
Share

@tormentoarmagedoom Thanks for the answer but i what do you mean by applying velocity when it touches the ground again?... I'm not sure i'm understanding what you mean. $$anonymous$$aybe i didn't write my question right : imagine my player jumping in one direction (let's say Z), then he moves his camera. The problem here is that my script makes the rigidbody rotate around the vertical axis (turning around) and the camera only around the horizontal axis (looking up and down). But i don't want my rigidbody's velocity to stay in transform.forward mode when i'm in the air.

Am i forced to make a separate script for the camera and never make the rigidbody turn is there any other solution?

avatar image tormentoarmagedoom Owlytsr · Jan 16, 2019 at 12:22 PM 0
Share

Good again.

I'm still not sure of what you want xd (my english is not very good) i did not read your codes (cant open the files) ,dont atach files, is better if u post the code here using the "Code Sampler Button".

As you say, you use transform.forward. This is a relative direction (depends on object rotation). If you pretend conserve the velocity, you need to do it in a global absolute coords. Every frame, the rigidboy knows its global absolute velocity, represented as a vector3 RigidBody.velocity This does not care about where the object is facing, or cameras or anything else.

If you read this value at the moment of jumping, you can then apply that velocity to the rigidboy again when you want, or apply only some opf the components of the vector.

for example, if charactter is moving (1,0,1) and then jumps (1,1,1) you can make something like this to conserve the direction of the jump:

 rb = GetComponent(RigidBody);
 float XcompBeforeJump = rb.velocity.x;
 float ZcompBeforeJump = rb.velocity.z;

Then force the rb to follow this velocity:

 rb.velocity = new Vector3 (XcompBeforeJump , rb.velocity.y, ZcompBeforeJump );

So, Y value will change (because the gravity) but X and Z compoentns will stay constant.

Bye!

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

215 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody AddForce(transform.forward) vs Rigidbody.AddRelativeForce(transform.forward) ? 1 Answer

Rigidbody velocity forward is Camera forward 1 Answer

C# keep previous velocity while jumping, previous solutions not working 0 Answers

controlles like leo fortune 0 Answers

How can I make my player jump faster but not higher? 3 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