Search Unity

Two connected characters influencing each other (overlaying movements)

Discussion in '2D' started by unity_voyager, Dec 2, 2019.

  1. unity_voyager

    unity_voyager

    Joined:
    Dec 2, 2019
    Posts:
    3
    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.
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.UpArrow))
    2.         {
    3.             UfoRB.AddForce(Vector3.up * UfoSpeed);
    4.         }
    5.         if (Input.GetKey(KeyCode.DownArrow))
    6.         {
    7.             UfoRB.AddForce(Vector3.down * UfoSpeed);
    8.         }
    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.

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.W))
    2.         {
    3.            CosmonautRB.AddForce(Vector3.up * CosmonautSpeed);
    4.         }
    5.         if (Input.GetKey(KeyCode.S))
    6.         {
    7.             CosmonautRB.AddForce(Vector3.down * CosmonautSpeed);
    8.         }
    9.  
    10. 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.
    Code (CSharp):
    1. if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    2.         {
    3.             Cosmonaut.transform.position = Vector3.MoveTowards(Cosmonaut.transform.position, new Vector3(Ufo.transform.position.x, Ufo.transform.position.y, Cosmonaut.transform.position.z), .02f);
    4.         }
    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! ^^
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    When you move your player object, also call the secondary player's Rigidbody2D and set it's velocity in the same direction the main player is moving. You can also make it follow the player by having the secondary player to always move towards the player's position. If you have no idea how this works, I can draw you up some code real quick. Lemme know! :)
     
  3. unity_voyager

    unity_voyager

    Joined:
    Dec 2, 2019
    Posts:
    3
    Oh I only just saw your answer now. Would be great if you could show some code to make clearer how this is done, as I am not quite sure. I will try myself until then and if I find a solution will add it here.
    Thank you already!
    Do both of these approaches work if first player influences the movement of the second player and also the other way around?
    Also I found this similar problem:https://answers.unity.com/questions/1226217/how-to-make-a-string-to-connect-2-object-not-strin.html but was not quite sure if this is a good solution for my rope?
     
    Last edited: Dec 7, 2019 at 4:00 PM
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    Well, I actually didn't get what you were saying originally. I thought you wanted to have a second player follow the first player. So if player1 moves left, the second player would also move left. However, now I see what you are trying to do. You want a game of "Tug of War" where the 2 characters pull each other as obstacles are in the way. I'm really not sure how that would work so I'd have to look into it and get back to you.

    What I need you to tell me is how will your rope work? Will it be a straight line or are you trying to achieve something more realistic?
     
  5. unity_voyager

    unity_voyager

    Joined:
    Dec 2, 2019
    Posts:
    3
    Oh sorry, I already thought that my description is not that good. But I am glad, that now it is clearer.
    Well of course a realistic rope would be cool but I think as this is my first real game a straight line would be fine.
    I don't know if it is important for you to know right now when thinking about the rope, but I should be able to detect collision of obstacles with the rope. Right now my rope is just done using a line renderer but if I keep this line renderer I am unsure if i could use a collider like on players or if this is not possible or good?
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    Well creating a realistic rope isn't that difficult actually. You can make one piece of rope and put an isTrigger collider on it for obstacle detection. You want a parent object, so create an empty game object, name it Rope, and reset its transform. Drag the piece of rope into the Rope empty to make it a child object. Then duplicate it over and over until you have the size rope you want. So now you can have motion with your rope, you will just need to insure they stay locked together as they move. To achieve this, make an empty game object on each side of the piece and use the Transform.position of the empties to lock together. Another method, I'm sure more popular, would be a hingejoint.

    Your question was okay, I simply misunderstood :p