top of page
  • Writer's pictureGreg Mendez

Swinging Mechanics


Honestly this whole system is really inefficient and weird, but I did spend a lot of time getting the movement to feel satisfying enough to begin to approach games like Spider-Man and Bionic Commando.


I was able to come up with a decent prototype of the swinging behavior by studying this blog post by the programmer who worked on Spider-Man 2 (PS2), Jamie Fristrom. He laid out a lot of great pseudo-code and examples for how to do proper calculations for moving a tethered object. He even includes a download link for his game, Energy Hook, which puts a lot of this stuff into practice in Unity. https://gamedevelopment.tutsplus.com/tutorials/swinging-physics-for-player-movement-as-seen-in-spider-man-2-and-energy-hook--gamedev-8782


Despite Jamie's awesome tutorials and examples, I wasn't able to just follow them directly and get decent results. I actually wound up restarting a handful of times and throwing out most of the prototype code before I landed on something that felt right to me.


I used Jamie's visuals as a guide, and started to implement a constraint system:





This is what I came up with to effectively constrain my character to a tether. This might not be the most efficient way of doing it, but this method gives me the best visual results.


Start

//Set the initial rope length in your start function

InitialRopeLength = (PlayerBody.position - Player.blackboard.GrappleHitPosition).magnitude;

//Grab Rigidbody

PlayerBody = Player.GetComponent<Rigidbody>();

...


Update

//Get a 'Test' position which predicts the player's movement over time

Vector3 TestGrapplePosition = PlayerBody.position + (PlayerBody.velocity * Time.deltaTime);


//Constrain the test position to the radius defined by the rope length

Vector3 Test = GrappleHitPosition + ((TestGrapplePosition - GrappleHitPosition).normalized * InitialRopeLength);

//Influence the player's velocity based on the calculated position

PlayerBody.velocity += (Test - PlayerBody.position).normalized;


//Move the rigidbody to the constrained position

PlayerBody.position = Test ;



Obviously there's a lot more involved in getting this to truly FEEL like Spider-Man, such as tweaking the camera's field of view and syncing everything up with animations. But this is the core of the grappling hook code that is the basis for the whole mechanic.



28 views0 comments

Recent Posts

See All
bottom of page