Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question (I need extreme help or bad things will happen with me) How do I create a game like Deepest Sword?

Discussion in 'Physics' started by Naomiin, Jun 20, 2022.

  1. Naomiin

    Naomiin

    Joined:
    Jun 4, 2022
    Posts:
    2
    I have a little more than 7 days to finish the beta of a game for a school project where everyone is counting on me, but I'm stuck, the game is totally based on Deepest Sword, and I need to exactly replicate the physics of this game, but I'm encountering a lot of problems:
    1- The sword box collider goes through walls
    2- Spinning the sword does not move the character forward
    Not only that, but I only managed to create the character and make the sword spin, I can't go beyond that, and I need to replicate the physics of this game quickly or I will disappoint all my teachers and my classmates, please help me.
    I'm new at game making, so I need a LOT of help, please.
    I will attach a gameplay of Deepest Sword that show exactly all the physics:
     
    Vincent454 likes this.
  2. Vincent454

    Vincent454

    Joined:
    Oct 26, 2014
    Posts:
    164
    So problem 1 and 2 seem to be related, the box collider goes through walls because it cant move the character wielding the sword. I have not played the game, but I guess you can't jump and have to use the sword to climb and get around. Could you maybe post some code of how you are currently doing things?

    The way I would set this up, just looking at the video would be to give the character a 2d rigidbody and the sword is under the character/a child of it in the hierarchy that also uses a rigidbody 2d.

    Give the sword a capsule collider 2d which will make collision smooth. You can also easily change its length that way, as you can modify the size of the collider. Also, I would increase the swords mass value every time it grows.

    The most important part though is: Use a joint for example a hinge joint 2d on the sword. Then set the Connected Rigidbody value to the character rigidbody.
    From script, you can enable the useMotor value if you want the sword to move and disable it again. You can set the motor torque and speed values to positive or negative values to have it rotate left and right.

    To make everything smoother to move and look at, enable rigidbody interpolation and make collision detection continuous. Also I am not sure, but maybe make the players collider a sphere collider to have it pass through corners more easily, also lock its rotation if you have not already done that.

    I hope this helps, dont worry about disappointing anyone, you got this!
    And even if it doesn't work perfectly at the end, this is quite a challenging system for someone just starting out even if it might seem simple.
     
    Naomiin likes this.
  3. Naomiin

    Naomiin

    Joined:
    Jun 4, 2022
    Posts:
    2
    Thank you very much, you helped me a LOT!
    But now I just encountered another wall, how do I control the hinge joint motor with the arrow keys? I imagine it's something like the character's movement, making the motor at the hinge joint go to 1 or -1 and multiplying by the Motor Speed.
    But I didn't find anything explaining how to program this
     
  4. Vincent454

    Vincent454

    Joined:
    Oct 26, 2014
    Posts:
    164
    Youre welcome! So in pseudo-code I would do:
    float direction = 0; //This will stay 0 if no button is pressed
    if(button to the left is held down)
    direction = -1;
    else if(button to the right is held down (Input.GetKey))
    direction = 1;

    if(direction == 0) //Not pressing a button
    joint.useMotor = false;
    else
    {
    JointMotor2D motor = joint.motor;
    motor.motorSpeed = direction; //Direction might be too little, maybe instead of -1 and 1 use a much higher number to push the player upwards more
    joint.motor = motor;
    //Need to reassign it, if youre a beginner this might be confusing, usually you would have been able to access motor.motorSpeed directly with joint.motor.motorSpeed but the way its setup in the background requires this with some Unity functions sometimes
    }

    I hope this helps, I have not tested this but thats what I would try
     
    tjmaul likes this.
  5. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    466
    I quickly implemented a working prototype because I really liked the gameplay. It also contains the control script for the motor that you were asking for.

    @Vincent13122 already gave you a good starting point by hinting to HingeJoint2d and you even knew about the hinge joint motor. But I don't believe you didn't find *anything* on how you program this. Plenty of tutorials on how to read player input and plenty of tutorials using HingeJoint2D. Please don't be lazy.

    Google search "hingejoint2d motor":
     
    Vincent454 likes this.
  6. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    466
    sorry, forgot to attach the file. Create a new 2D unity project and import this file. The scene is called "Scenes/Test Scene"
     

    Attached Files:

    Vincent454 likes this.