Hello there,
I am new to Unity and currently working on a small 2D game in which two players can play together.
Each of them should be able to control one of the two characters which are connected with a rope.
Each player can move his character up and down and one of them can also make the connection between the two characters smaller (like pulling the other person closer → so at the end the goal is to pull one character to the other while both have to avoid moving obstacles).
The first thing that seems difficult right now is how to implement this concept so it feels nice when playing.
When the first character is moved up, the second one should also get some of that upwards movement as they are connected through the rope. Of course the second player should be still able to influence its movement.
At the moment the first character is moved with the arrow keys and by using a force on its Rigid Body like so.
if (Input.GetKey(KeyCode.UpArrow))
{
UfoRB.AddForce(Vector3.up * UfoSpeed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
UfoRB.AddForce(Vector3.down * UfoSpeed);
}
At the moment my code for the second player kind of works but the two movements oftentimes do not overly nicely. I think the reason is that I am mixing concepts on how to move stuff. At the one hand i am using Force, on the other hand I am also changing the players position by moving towards the same y coordinate as the first player using Lerp. But this was the only way I was able to do it right now.
if (Input.GetKey(KeyCode.W))
{
CosmonautRB.AddForce(Vector3.up * CosmonautSpeed);
}
if (Input.GetKey(KeyCode.S))
{
CosmonautRB.AddForce(Vector3.down * CosmonautSpeed);
}
Cosmonaut.transform.position = Vector3.Lerp(Cosmonaut.transform.position, new Vector3(Cosmonaut.transform.position.x, Ufo.transform.position.y, Cosmonaut.transform.position.z), .03f);
I would be glad about tips on how to better implement this feature!
Furthermore I have issues with the connection between the players.
At the moment the rope is done using a simple line renderer. If the mouse wheel is scrolled, the second player should move towards the first one.
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
Cosmonaut.transform.position = Vector3.MoveTowards(Cosmonaut.transform.position, new Vector3(Ufo.transform.position.x, Ufo.transform.position.y, Cosmonaut.transform.position.z), .02f);
}
Which does the job of pulling in the other character, but as soon as the players move, the length of the rope changes again.
How can i make the the rope of constant length while moving both characters and only change the length if the mouse wheel is scrolled?
I hope I was able to describe my problems. Thanks for every hint in advance! ^^