Spiderman's web swinging's Vector Math

I’m trying to script web swinging but having trouble in vectors

I thought of this to create my linear velocity

-- |||||||
|---|---|---|---|---|---|
||||||local Velocity = Instance.new(LinearVelocity)|
||||||Velocity.Name = SwingForce|
||||||Velocity.Parent = HumRootPart|
||||||Velocity.Attachment0 = HumRootPart.RootAttachment|
||||||Velocity.MaxForce = 1000000|
||||||Velocity.VectorVelocity = Camera * 10000|

but this linear velocity causes player to go down when he looks down but spiderman cant swing down.

I need help on the vector math of this, do you have any ideas?

You could do it fully within the physics engine. If you know the AssemblyMass of your character, then you can apply a constant force towards wherever the attachment point of the web is that equals the weight (mass * gravity) of your character. You’d probably have to disable movement while you’re swinging so the character controller doesn’t interfere, but it’s an option.

You could also use SpringConstraints potentially?

Update:
I did write this which is kinda better but still janky

any other suggestions?

I have no idea what those are, But the first thing you recommended would be a grapple, not a swing I assume

Lemme check those concepts

Oh I did the web with beams, I dont think springs will work

You can visualise with the beam, but utilise constraints (spring/rope) to handle the physics.

This is of course depending on whether or not you want your spiderman to swing ‘realistically’. If you just want it to pull in the direction of the camera then take the lookVector’s X/Z coordinates:

local NormalizedLookVector = Vector3.new(Camera.CFrame.lookVector.X, 0, Camera.CFrame.lookVector.Z).Unit;

To get the direction of travel on the horizontal plane. You could create an ‘imaginary point’ at some distance above and in-front of the camera (along that lookVector) then there’s a few ways you could approach it.

  1. Constraints/Forces (like I demonstrated previously)
  2. A single impulse velocity (similar to what you did)
  3. Some sort of mathematical pendulum function to get the tangential acceleration vector at each point during the swing, something like:
local radialVector = (HumanoidRootPart.Position - webAttachment.Position);

local upVector = Vector3.new(0, 1, 0); -- 'up' in world-space
local tangentialVector = radialVector:Cross(upVector).Unit; -- The direction the character should accelerate at that point during the pendulum

image

If you combined the above with a rope constraint (with winch to ‘pull’ the character towards it?) and updated a VectorForce/BodyVelocity every physics step using that tangentialVector then you should be able to get a decent amount of control over how fast the swing is. Take care though, because once the character passes underneath the attachment point the unit vector will invert and start pointing backwards (as the direction the character should be accelerating towards is now behind).

If you just want it to pull towards a point:

local Direction = (Position1 - Position2).Unit;
1 Like

I saw this very late. Sorry
this is a nice explanation and exactly what I was asking for!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.