- Home /
Help with coding a simple grapple system - javascript
I don't have much experience coding in java, but I'm familiar with coding itself, as I'm an expert at actionscript3. I need to make a simple-ish dual grapple system (as seen in "Attack on Titan" see link---> 3D Maneuver Gear - Grapple system in action - jpeg) to move around an environment.
I'm planning on using a rope prefab, basically a thin and long cylinder that you can adjust the length of with code, and a third-person character prefab. So here's how this will work:
the player's grapple gun is point A. When a button is pressed, the rope prefab appears in the direction of the crosshair and plays an animation of a shot. the length of rope prefab is always in front of the gun itself, but i think that can be handled with animations.
when (and if) the prefab hits a tagged object, it replaces itself with another of the same rope prefab that connects point A (the grapple gun) to point B (the object). this new prefab is the same prefab from before, but the length is the distance from point A to point B and there are no active animations.
next, force is applied to the player in the direction of point B, but the force is included with (or "on top of") all the other forces affecting the player (wind, jumping or running, explosions, the player's own weight/gravity, etc), and the player (which the grapple gun is a child of) is moved along the rope prefab to point B. All the while, the rope is getting shorter by frame according to the player's distance from A to B. (this can be set by one line of code updating each frame, or a function executing only when grapple key is pressed to when point B is reached)
//note that point A will always be the gun, and point B will always be a tagged object.
I just need a little bit of pointers in the right direction, if this is possible or not, if some elements should be revised or scrapped. If it's possible, actual code will help alot. Note that the verbal schematic is only for one grapple gun, and there will be two per player, so the variables might need to be named accordingly. The gun is a child of the player, not the player itself.
Thanks for any feedback
Note, that "Java" is not the same as "Javascript". They are two completely different program$$anonymous$$g langes and should not be confused with oneanother :)
ALso when making references, it's good to show a link or something. Just saying "like Attack on Titan" will not help someone understand it. Post an image or something, or even link a video ;)
for good question and the fact you edited ins$$anonymous$$d of posting a comment
Users who do good should get rewarded properly :)
Answer by skylem · Jan 19, 2015 at 07:42 AM
K fellow here it is Some Pointers hope i provide enough, if i were to make a grapple system i would first ask myself the question of how im going to achieve this and discern what objects/how many and there components i might need to do this So after about 30 seconds Heres a rough concept for the Grapple System This is Only examples of the code you may need/wish to use, as to write the entire script would probably take me several hours Good Luck i hope it all helps.
Transform grappleHook; //Reference your hook
Transform character; //Reference what ur dragging
bool fired; // check if you fired the hook
bool madeContact; // check if the hook made contact
RaycastHit hit1 = new RaycastHit(); // create a raycast for the contact point this is set by out below
Vector3 CollPoint1 = grappleHook.transform.TransformPoint( grappleHook.center ); // create and set vector3 for the raycast
if( Physics.Raycast( CollPoint1, out hit1, 1f )) {
madeContact = true;
}
//check a keypress to shoot it
if(Input.GetKeyDown(Keycode.Space)) {
fired = true;
}
// use them together to create your own function
if(fired && madeContact) {
GrappleHook();
}
// use this function to move check the distance between the object ur moving and your contact point.
void GrappleHook() {
float distance = Vector3.Distance(hit1.transform.position, character.transform.position)
// use the distance to say the character should move toward the hit point
if(distance > 2) {
// move it with a lerp for a smooth transition over frames
Vector3.Lerp(character.transform.position, hit1.transform.position, 0.1f);
}
}