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 /
  • Help Room /
avatar image
1
Question by xxchester · May 16, 2021 at 12:36 AM · physicspositionvelocity

How to combine jump and x movement using velocity and transform

Hello, I have a problem that I just cannot seem to figure out. Basically I am writing a networking PoC and when I use this code to move, both server and client receive the movement properly.

 float x = MathUtils.FloatToInt (Input.GetAxisRaw ("Horizontal"));
 if (x <= -1) {
       UpdatePositionServerRpc (PlayerActions.MoveRight);
  } else if (x >= 1) {
       UpdatePositionServerRpc (PlayerActions.MoveLeft);
  } 
 rigidbody.transform.position = newPosition.Value;

When I use this code without changing the position with the above code, both server and client jump up and down correctly.

 JumpServerRpc ();
 rigidbody.velocity = newVelocity.Value * Time.deltaTime;

However, the problem is how do I combine these two to work? Anytime I add in the position change snippet, the jumping stops working. I have tried using add force and it's variants but they all have issues starting the jump (have to mash the space bar) and jitter once they do jump.

  // jitters when it eventually starts jumping, gotta hit jump many times
  // JumpServerRpc ();
 // rigidbody.AddForce (newVelocity.Value * Time.deltaTime, ForceMode.Impulse);
 
 // jitters when it eventually starts jumping, gotta hit jump many times
 / JumpServerRpc ();
 / rigidbody.AddForceAtPosition(
 //     newVelocity.Value * Time.deltaTime,   rigidbody.transform.position, ForceMode.Impulse);
 

Any help on how to combine these two would be greatly appreciated. Thanks.


I should also add, when I use strictly velocity based movement and jumping it works as expected but my model rotates all over the place and also slides when no input is given. The calculations for jumping and change in position are very straight forward, I have included them here incase someone wants to see them.

 void UpdatePositionServerRpc (PlayerActions playerAction) {
     float speed = 10f;
     Vector3 newPosition = rigidbody.transform.position;
     // Debug.Log ("action: " + playerActions.Value);
     if (PlayerActions.MoveRight == playerAction) {
         newPosition.x = speed;
     } else if (PlayerActions.MoveLeft == playerAction) {
         newPosition.x = -speed;
     } else if (PlayerActions.Idle == playerAction) {
         newPosition.x = 0f;
     }
     float delta = newPosition.x * Time.deltaTime;
     this.newPosition.Value += new Vector3 (delta, 0f, 0f);
 }
 
 [ServerRpc]
 void JumpServerRpc () {
     newVelocity.Value = new Vector3 (0f, 400f, 0f) - rigidbody.velocity;
 }




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

Answer by xxchester · May 16, 2021 at 12:52 PM

So I found the issue, it was really stupid. I was not initializing this.newPosition.Value to the characters current position. As soon as I did that, everything started working using both addForce and movement.

Thanks.

Comment
Add comment · 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

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

249 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 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

Falling Platform 1 Answer

[UNET] Rigidbody physics sync 0 Answers

Bounce, gravity and velocity 1 Answer

Gun RayCast Origin lags with fast moving object. Help? 0 Answers

Rigidbody rotate velocity 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