Spaceship controls inverting when i move my ship

when turning my spaceship to different angles a becomes forward, d becomes backward, w becomes left and so on, this is because when the bodygyro changes the angle of my ship the method of moving my ship ceases to function.

https://gyazo.com/541c3895034f957610611d0a365ce7a6

bp.Position = Vector3.new(bp.Position.X + xDirect,bp.Position.Y + yDirect, bp.Position.Z + zDirect)

becauase the angles have changed, simply adding a -1 or 1 onto the movers current position does not work anymore.

im now left wondering what the best way to proceed is, ive tried looking into lookvectors,upvectors and rightvectors but to no avail.

All help appreciated :+1:

2 Likes

You probably haven’t used them correctly, they are very powerful. If you want the ship to go forward when you press W and backward when you press S, simply assign a ‘1’ to W and a ‘-1’ to S, and take the ship seat’s CFrame.LookVector and multiply it by that 1 or -1 depending on the key you’re pressing. Multiply this then by the ship speed you want and use that in the velocity of whatever bodymover/constraint you’re using to make the ship move.

Currently using bodyposition, would it still be compatible?

I don’t use BodyPositions but my guess is that what you’d do instead is take the new velocity component from my last post and add it to the seat’s current position, then use this for the BodyPosition.

what bodymover do you recommend for this sort of thing?

well ive used bodyvelocity and im getting the same results

https://gyazo.com/3542c8e131491e20a095c9dece3aa211

same inverted camera, still not going in the direction i set.

im obviously doing something wrong :stuck_out_tongue:

local rVec = part.CFrame.RightVector.X + xDirect * speed 
local uVec = part.CFrame.UpVector.Y + yDirect * speed
local uVec = part.CFrame.LookVector.Z + zDirect * speed
bv.Velocity = Vector3.new(rVec,0,uVec)

Are you looking to make the spaceship controls relative to how the camera is facing like how the normal humanoid Robloxian character walks along the floor?

If so we can steal some code from @ThanksRoBama to get movement vectors that are relative to the camera.

--in a local script to detect player input
local InputS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

local walkKeyBinds = {
	Forward = { Key = Enum.KeyCode.W, Direction = Enum.NormalId.Front },
	Backward = { Key = Enum.KeyCode.S, Direction = Enum.NormalId.Back },
	Left = { Key = Enum.KeyCode.A, Direction = Enum.NormalId.Left },
	Right = { Key = Enum.KeyCode.D, Direction = Enum.NormalId.Right }
}

local function getWalkDirectionCameraSpace()
	local walkDir = Vector3.new()

	for keyBindName, keyBind in pairs(walkKeyBinds) do
		if InputS:IsKeyDown(keyBind.Key) then
			walkDir += Vector3.FromNormalId( keyBind.Direction )
		end
	end

	if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
		walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
	end
	
	return walkDir
end

local function getWalkDirectionWorldSpace()
	local walkDir = camera.CFrame:VectorToWorldSpace( getWalkDirectionCameraSpace() )
	walkDir *= Vector3.new(1, 0, 1) --Set Y axis to 0

	if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
		walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
	end

	return walkDir
end

Then after the above-copied piece of code sets up the functions for obtaining the movement on the XZ plane relative to the camera.

Then we can set the body velocity to the walkDir obtained through the following functions.

bv.Velocity = getWalkDirectionWorldSpace() * speed

Edit: Otherwise I would use multiplication in order to make the movement relative to the part CFrame.

local rightVec = part.CFrame.RightVector * xDirect * speed 
local upVec = part.CFrame.UpVector * yDirect * speed
local lookVec = part.CFrame.LookVector * zDirect * speed
local netMovementDirection = lookVec+upVec+rightVec
bv.Velocity = netMovementDirection * Vector3.new(1,0,1)

1 Like

Uhh, just curious which solution did you use and wanted for your spaceship?

i used and wanted the second one! Thank you.