- Home /
The question is answered, right answer was accepted
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??
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?
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
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.
Sorry, I am a complete noob to networking and stuff. But where do I have to put that line of code?
@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.
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
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);
Follow this Question
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