Search Unity

Gameplay questions

Discussion in 'Scripting' started by Deleted User, Feb 20, 2011.

  1. Deleted User

    Deleted User

    Guest

    Hello. What would be the best way to create a swingning mechanic that works just like this? I want to create something where the player shoots out a hook, the hook attaches to some type of object and the player can swing on it, (the game I want to make is also in 2D).

    How can I have a joint be spawned at another place and attached to the player object? Would the "shooting out" of the grappling hook actually happen, or would it be easier to have it just an animation(I still want the hooks distance to be variable depending on when it hits something)? What about reeling in the hook, is there a way to do that with hinges? The hook doesn't need to be able to shoot out all around the player, just straight ahead, above and at a 45 degree angle.

    The swinging is fairly simple(the game I showed is a actually a remake of a NES game, would I even bother with hinge joints (would there be a way to code the swinging)? I have tried messing around with hinge joints but their movement has been fairly erratic, I want a nice smooth and simple swing (which is why I wonder if I am complicating things too much by using actual physics).

    I know that is alot of questions, but thanks.
     
    Last edited by a moderator: Feb 21, 2011
  2. Deleted User

    Deleted User

    Guest

    I know it is alot of question, but I am lost. I dont even know if it would be better to go with actual physics or to just script it all.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    If you are building a 2d scroller like that. I would think you would need two layers, first, a layer with the colliders for the ground, second a layer for the player and mobs he is combating.

    next, do a Plane.Raycast from the click point, or touch point, if you have it to the point on the screen eminating from the character. (if you are just using directions a ray is easy to get from that)

    In the shoot phase, you would want to shoot a hook along a coroutine and check it against the ground layers towards the point where you touched the screen, or however you got your rayHit. That point, is where you create your Hook's Joint. Then just use the physics of the joint to manage your character. Your joint, would then be parented to the world (null) and the connected body would be the character at the gun point.

    Oddly enough, someone just posted a link today to the FPS Rigidbody walker, which could be redone to a side scroller walker without much trouble.

    http://www.unifycommunity.com/wiki/index.php?title=RigidbodyFPSWalker
     
  4. Deleted User

    Deleted User

    Guest

    How would I deal with the application of force to the swinging? I have been able to get a swinging effect by having a constant force and then manually deactivating it while the scene is running, but thats not efficient (or practical)? Also, is there a way to create a "reel in" type effect (so if the player presser a button, the character will be pulled towards the joint)? How would I instantiate the hook object at the point where the raycast hits?
     
  5. Deleted User

    Deleted User

    Guest

    I have also run into another problem. The hook would be an an angle to the player, but when I position two objects diagonally from another (so the player object is at 0,0,0 and the joint is at 5,5,0) when it swings, it acts strange. It only seems to swing somewhat normally when I put the joint object directly above the player object).I know its alot of work, but if someone could please post a web player version or unity package example so I could see what you mean.

    Here is my test so far. Its just something swinging. Press D to add force from left to right, press D to add force from right to left.
     

    Attached Files:

    Last edited by a moderator: Feb 22, 2011