How do I move the player forward for combat

Hi, I want to know how I can move the player who slashed someone forward and the enemy thats being slashed backward. I have the mechanics done and I just want to know the method used in this video:

You can see the sphere moving forward smoothly every slash

3 Likes

You can probably have it be animation driven, or have some sort of sine movement that you’d calculate over runtime.

I guess as a starter, something like this:

local max_distance = 5 -- // Studs
local play_duration = 0.35 -- // Seconds
local move_direction = Vector3.zAxis -- // the character's look/attack direction
local PI = math.pi
local sin = math.sin
local start_time = os.clock()

local function toSine(amplitude : number, alpha : number) : number
    return amplitude * sin(alpha * PI)
end

local c : RBXScriptConnection
c = RunService.Stepped:Connect(function()
    local alpha : number = (os.clock() - start_time)/play_duration
    local added_distance : Vector3 = move_direction * toSine(
        max_distance, 
        math.min(alpha, 1)
    )
    -- // Apply to your root part or something

    if (alpha >= 1 then c:Disconnect(); return end
end

The calculations are completely up to you though… You can also do this with acceleration & velocity, or just a basic sine / cosine like here.

1 Like

How do I make the enemy move back the same amount?

Do the same thing but with the other character…

2 Likes

You can use a very powerful BodyVelocity and use Debris to make it only last a very very short amount of time. This is what I do in my game to create a forward attack.

I think I’m doing this wrong… It moves me too far and moves my enemy down to the floor

self.RightHitbox.OnHit:Connect(function(hit, humanoid)
		if humanoid == self.Humanoid then
			return
		end
		
		local enemyRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
		
		HitBridge:Fire(self.Player, humanoid)
		
		Connection = RunService.Stepped:Connect(function()
			local alpha : number = (os.clock() - START_TIME)/PLAY_DURATION
			local added_distance : Vector3 = MOVE_DIRECTION * toSine(
				MAX_DISTANCE, 
				math.min(alpha, 1)
			)
			
			self.Root.Position = (self.Root.Position * added_distance)
			enemyRoot.Position = (enemyRoot.Position * added_distance)
			
			if (alpha >= 1) then Connection:Disconnect(); return end
		end)
		
		PlaySound(self.Sounds["Hit"], hit, math.random(10,20)/10)
		humanoid:TakeDamage(self.Settings.M1_DAMAGE)
	end)
	
	self.LeftHitbox.OnHit:Connect(function(hit, humanoid)
		if humanoid == self.Humanoid then
			return
		end
		
		local enemyRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
		
		Connection = RunService.Stepped:Connect(function()
			local alpha : number = (os.clock() - START_TIME)/PLAY_DURATION
			local added_distance : Vector3 = MOVE_DIRECTION * toSine(
				MAX_DISTANCE, 
				math.min(alpha, 1)
			)
			
			self.Root.Position = (self.Root.Position * added_distance)
			enemyRoot.Position = (enemyRoot.Position * added_distance)
			
			if (alpha >= 1) then Connection:Disconnect(); return end
		end)
		
		PlaySound(self.Sounds["Hit"], hit, math.random(10,20)/10)
		humanoid:TakeDamage(self.Settings.M1_DAMAGE)
	end)

It’s added distance. You’re currently multiplying the character’s position rather than adding onto the position.

But when I add the vectors, they don’t do anything…

Is your MOVE_DIRECTION variable, the attacker’s move direction? Maybe switching that to the root part’s look direction or the direction between attacker and receiver.

Is there such a thing as negative lookvector or perhaps EnemyMoveDirection = player.Character.LookVector * 180 degrees or something.

Sorry this isn’t a code rather an idea I can’t do with math.

-CFrame.LookVector is the opposite direction.

Then perhaps OP can do that in unison to each other, player and target cframe and -cframe.