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 /
This question was closed Dec 27, 2016 at 07:39 PM by wesleywh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by wesleywh · Dec 19, 2014 at 11:28 PM · multiplayer-networkingsmoothing

Smooth Multiplayer Movement(Vector3.Lerp)

So everything that I have been reading tells me that I need to use Vector3.Lerp to smooth my player movement. I know it should be like:

 Vector3.Lerp(transform.position, currentposition, 0.1f);

However, I don't know how to get the "currentposition" in the above example. I have heard of OnSerializeView() but isn't that just a Photon thing? I am not using PhotonNetworking in my game. Any help on how to get that updated position would be greatly appreciated.

EDIT: So I tried the following:

 Vector3.Lerp(this.transform.position, this.GetComponent(NetworkView).transform.position, 0.1f);

this works a little but I still get jitter. Is there a way to improve this code??

Comment
Add comment · Show 5
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 wesleywh · Dec 21, 2014 at 04:54 AM 0
Share

So this code works for you? I am just reading your code and it looks like it says move from my current position to my current position. I disable the rigidbody controller and the rigidbody is kinamatic if it isn't my networkView. So could I implement this code in a new script that is active on a player that isn't $$anonymous$$e to update the position smoothly?

avatar image pako · Dec 21, 2014 at 07:23 AM 2
Share

No, actually the code doesn't say that. The code says:

Calculate a position which is 10% of the way between the previous frame's current position and the destination position, and then set that calculated position as this frame's current position. The Lerp function with 0.1f as 3rd parameter does the calculation, and the result is assigned as the new transform.position.

To be fair, I can see that this can be confusing, because transform.position is in fact this frame's current position. However, after the calculation and assignment transform.position changes a little bit towards the target... every frame... and that results in smooth motion. In fact, at the beginning of each frame, the object's transfer.position will be the position corresponding to the previous frame's Lerp calculation.

You can use this code to move any object smoothly towards a target. Of course, since you mentioned Rigidbody, I have to say that if you apply a force using the Rigidbody component, that force would affect the position of the object, so, smooth movement with Lerp will be affected. But there could probably be cases where you'd want that, e.g. you could be moving an object with Lerp, and then add a force to the object's Rigidbody if game action requires that you hit that object somehow. Rigidbody can also be useful for gravity. So, you can use Lerp movement and rigidbody together, if you wish, but keep in $$anonymous$$d that they can affect each other. If you don't have a Rigidbody, or if the Rigidbody is kinematic, then the only option you have for movement is to change the object's transform.position, and of course Lerp provides smooth movement.

However, I prefer to use a tweening plugin for smooth movement. They provide for more realistic movement, such as decelerating as the object is close to its destination etc:

https://www.assetstore.unity3d.com/en/#!/content/84

https://www.assetstore.unity3d.com/en/#!/content/3595

https://www.assetstore.unity3d.com/en/#!/content/3311

avatar image wesleywh · Dec 21, 2014 at 11:23 PM 0
Share

I actually miss read your code I thought where it said "target" .position I thought it said "transform" .position. Thank you for these other options I will have to study a little more about them since I don't know anything about them. EDIT: Also I can't mark your answer as a correct one since you have it as a comment.

avatar image Wiebren-de-Haan · Mar 30, 2015 at 03:10 PM 0
Share

Sorry, I am a complete noob to networking and stuff. But where do I have to put that line of code?

avatar image pako · Mar 30, 2015 at 04:23 PM 0
Share

@Wiebren_de_Haan you put it inside the Update() method of the $$anonymous$$onobehaviour script, which is attached to the GameObject you want to move.

@wesleywh I just converted my comment to answer so you can mark it as accepted. Sorry it took me so long, but I hadn't read properly your edited comment.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by wesleywh · Apr 12, 2015 at 08:58 PM

I have given a very in-depth explanation on this one. While pako eventually lead me to the right answer it wasn't really producing the correct effect that I am looking for. I have given this full answer at the other question that I asked:

http://answers.unity3d.com/questions/913401/smooth-multiplayer-with-vector3lerp.html

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

Answer by pako · Dec 20, 2014 at 08:59 PM

Maybe I don't understand well what your problem is, because to my understanding you need to use target.position and not currentposition as you describe. Here "target" is the Transform of where you want to move. You could also use "target" as a Vector3 location, in which case you'd need to use just target instead of currentposition.

In any case, I understand that currentposition is actually transform.position, so, maybe I don't understand your problem well.

Update():

 transform.position = Vector3.Lerp(transform.position, target.position, 0.1f);

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

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Multiplayer: jitter caused by camera? by smoothing? 2 Answers

Gradually reduce speed 1 Answer

Rigidbody 2d smooth movement problem (It's jerky) 1 Answer

How do i make multiplayer for my game? 0 Answers

Network lag Causes enemy player to die twice. 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