CFrame sometimes goes where I don't want it to go

Hello!

So, I need some help on this death system that I am creating. Some of it has to do with CFrame. Let me get to the point, the point is, is that everytime the character would move a lot, and when the death function would trigger, the CFrame would push the player into another coordinate frame of the game, basically making the character face backwards instead of forwards, which is not what I want.

I have some videos that should put things in context and make the situation more understandable. :

Whats happening in this video is that when the player is rapidly moving while the death function triggers, what happens is instead of the character’s CFrame moving infront of the player’s screen, the character’s CFrame moves at the back of the screen. I don’t know if there is any fix for this? If so, I am curious.

robloxapp-20230504-0829216.wmv (317.9 KB)

The last video I have is of the character not rapidly moving while the death function triggers. If the character stays still, then the character will pop up infront of the screen, else the character will go to the back of the screen, which is what the first video is about. This video is also what I want the player’s death to be all the time, infront of the screen, not at the back of the screen.

robloxapp-20230504-0855261.wmv (224.1 KB)

(If you need me to elaborate more on the problem, please let me know.)

Finally, here is the code. This code is stored inside of a ModuleScript. This code is the program for making the death happen.

local deathmdle = {
	tme = .5,
	tmeinout = .3,
	
	othermaxforce = 99999999,
	huge = math.huge,
	
	waime = .55,
	
	velocity = 150
}

local player = game:GetService("Players").LocalPlayer
repeat task.wait() until player.Character

local character = player.Character or player.CharacterAdded:Wait()
local animations = script:WaitForChild("ANIMATIONS")

local hrp = character:WaitForChild("HumanoidRootPart")

local controls = require(player.PlayerScripts:WaitForChild("ControlScript")["GAMEPADS/KEYBOARD"])
local camera = require(player.PlayerScripts:WaitForChild("CameraScript").CameraControl)

local sounds = workspace.sounds

local t1
local t2

local anim = {
	death = character:WaitForChild("Humanoid").Animator:LoadAnimation(animations.death_anim)
}

function deathmdle.CharacterDeath (self)
	task.wait()
	
	local self = deathmdle
	controls.DisableController()
	
	for i,v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
		v:Stop(0)
	end
	
	local deathvl = Instance.new("BodyVelocity", hrp)
	deathvl.MaxForce = Vector3.new(0,self.othermaxforce,0)
	deathvl.Velocity = Vector3.new(0,0,0)
	
	t1 = game:GetService("TweenService"):Create(deathvl, TweenInfo.new(self.tme), {Velocity = Vector3.new(0,self.velocity,0)})
	t2 = game:GetService("TweenService"):Create(deathvl, TweenInfo.new(self.tmeinout), {Velocity = Vector3.new(0,-self.velocity,0)})
	
	for _, v in pairs(character:GetChildren()) do
		if v:IsA("BasePart") or v:IsA("Part") then
			v.CanCollide = false
		end
	end
	
	for _, v in pairs(character:GetChildren()) do
		if v:IsA("Accessory") then
			for i, parts in pairs(v:GetChildren()) do
				if parts:IsA("Part") then
					parts.CanCollide = false
				end
			end
		end
	end
	
	sounds.death:Play()
	anim.death:Play(0)

	hrp.CFrame *= CFrame.fromEulerAnglesXYZ(math.rad(hrp.Orientation.X),math.rad(-90),math.rad(hrp.Orientation.Z)) * CFrame.new(0,0,-10)
	
	task.wait(self.tme)
	deathvl.MaxForce = Vector3.new(0,self.huge,0)
	
	t1:Play()
	t1.Completed:Wait()
	t2:Play()
	
	wait(self.waime)
	camera.DisableCamera()

	return self
end

return deathmdle

I appreciate all kinds of help. Thank you.

1 Like

I am not really sure what’s the error in your script because as far as I’ve read everything, nothing seems to be a problem… I think the problem arises by the coordinate system you’ve used in this line

hrp.CFrame *= CFrame.fromEulerAnglesXYZ(math.rad(hrp.Orientation.X),math.rad(-90),math.rad(hrp.Orientation.Z)) * CFrame.new(0,0,-10)

I’ve watched both of your videos and it seems like the character flies up and jumps out of the map, In order to not let the character jump out of map, you could add in an invisible wall at both of the sides of the map…

1 Like

I do want the character to jump out of the map, I just don’t want the player to go at the back of the map, or screen.

This is how I want the character to look like when it jumps out of the map when you die:

I don’t want the player to keep going the opposite way like seen here:

I fixed it! Thank you for helping me a bit.

I came up with this line, which solved everything. All I have to do is find a way to rotate the player to look at the camera.

hrp.CFrame = CFrame.lookAt(Vector3.new(workspace.CameraPart.Position.X,workspace.CameraPart.Position.Y,workspace.CameraPart.Position.Z - 50), hrp.Position) * CFrame.new(0,0,-self.outtervalue)

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