I’ve been stuck on this for almost a week now and haven’t quite found a solution that works for me, so thought I’d finally ask a question here…
The Context:
I have two Dynamic Rigidbody2D characters attached to each other by a Distance Joint 2D.
Their left and right movement is controlled by using:
Public float speed;
moveInput = Input.GetAxisRaw(“Arrows”);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y));
The Issues
This works really well, BUT using ‘rb.velocity’ to control the characters’ movements cancels out any physics, which are necessary to get the players ‘swinging’ when they aren’t grounded and being dragged by the other player on a higher platform…
I’ve tried replacing the code with the following, which gets the swinging effect, but the characters pick up too much speed when they walk and especially when they jump/fall, and their movement is too slow when they first start walking
public float speed;
moveInput = Input.GetAxisRaw(“Arrows”);
rb.AddForce(new Vector2(moveInput * speed, rb.velocity.y));
I’ve tried using a maxSpeed to stop them going too fast, as well as ForceMode2D.Impulse to make the initial speed faster, but nothing I did was as good for controlling the character as using .velocity.
The Current Solution:
Right now I have something that kind of works, but I’m worried it will affect performance because it lags sometimes when I playtest…
(In Fixed Update)
Private float distance;
Public float distanceThreshold;
Public float swingSpeed;
distance = transform.position.y - otherPlayer.position.y;
if (distance < distanceThreshold && isGrounded == false)
_{
_
_rb.AddForce(new Vector2(moveInput * swingingSpeed, rb.velocity.y));
_
_}
_
_else
_
_{
_
_rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
_
_}
_
The Questions:
-
How can I move the characters using AddForce but make their movement as snappy, and constant, and as similar as possible to using .velocity ?
-
Is my current solution really terrible for performance, or will it cause other issues I’m unaware of?
-
Is there perhaps another/better/simpler way to achieve what I’m trying to do?
This is the game I’m expanding on if it helps with the context… You can see how the players don’t exactly ‘swing’ how they should and when they should…
Thanks in advance!!