How to convert unit vector into CFrame for use in BodyGyro


#1

I really hate having to ask questions here, because I like to solve problems without having to make people solve them for me but this is really beginning to frustrate me now because I have zero clue what to do.

If you’ve seen this then you know I’m working on a thing that simulates gravitational attraction to a planet, but I had a little optional thing there that asked how to keep the character pointing like thismspaint%20example%20lolol and I got a great answer for all of my questions but the person who gave the solution said to use BodyGyros to keep the character upright.

Now this seems to me like a great answer but I don’t know how to use the unit Vector in the BodyGyro, because it’s a Vector3 and not a CFrame, and I don’t know how to convert one into the other.

Here’s my code, yes, it’s a mess.

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local hrp = character.HumanoidRootPart
local attachment = Instance.new("Attachment", hrp)
local lineforce = Instance.new("LineForce", hrp)
lineforce.Attachment0 = attachment
lineforce.Attachment1 = workspace.Planet.Planet
lineforce.ApplyAtCenterOfMass = true
lineforce.Magnitude = 1000
local gyro = Instance.new("BodyGyro", hrp)
gyro.MaxTorque = Vector3.new(650000,0,650000)
gyro.P = 650000
gyro.CFrame = -- now this is where i'm struggling
end)
end

#2

To convert a Vector3 into a CFrame, simply do

local cf = CFrame.new(vector)

To get the position of a CFrame just do

local vector = cf.Position

#3

error


#4

I would strongly recommend using indentation in your code. You can do this by pressing the Tab key while your cursor is at the start of a line of code:

print("Hi!") -- before
    print("Hi!") -- after

You can also select multiple lines and press Tab to indent them all at once:

-- before
for x=1, 20 do
    print(x)
end

-- after
    for x=1, 20 do
        print(x)
    end

#5

yeah i’m aware, i never meant for this to be public though so i didn’t modify it.


#6

So as I understand it, you have an unit vector, which you want to use for the rotation of a CFrame, right?

If so, you can use the CFrame.new(position, lookAt) constructor, of which both position and lookAt are Vector3s.
Also note that BodyGyroes won’t care about their CFrame’s position, only its rotation.

local unitVector = Vector3.new(0, 0, 1) -- example unit vector.

local cf = CFrame.new(Vector3.new(), unitVector) -- creates a new CFrame looking towards unitVector.

As for your reply to Faultsified, you might want to supply the code you used when you got that error - the error alone isn’t enough to clarify the issue in this case :slightly_smiling_face:

References


#7

Here’s the code, the while true do was just a test to see if the problem was that it wasn’t looping, I am aware of how inefficient it is.

while true do
		wait(0.01)
		gyro.CFrame = ((workspace.Planet.Position - hrp.Position).unit)
		end

#8

Oh, NICE. I just realized the problem.


#9

Okay, it works for the most part, however, I ran into an issue.

The HumanoidRootPart is pointing in the right direction, but the wrong face is pointing.

The front face is pointing in the direction, not the top, which is what I’m looking for.

Any fix?


#10

I tried to make a planet like this yesterday and stumbled on the exact same problem. Waiting for a reply :eyes:

:eyes: :eyes: :eyes:


#11

lol yeah, CFrames are not my thing.


#12

Got a screenshot / video of the issue?
Also, I don’t think just using a single vector3 will suffice as to how the character is turning on the rotated surface, if that’s your goal. There’s also to consider the issue with gimbal lock when using the CFrame constructor I suggested before, so that won’t work if your goal is to make a player able to walk around on a sphere.

If the issue is simply that the character is upside-down, you can invert your unit vector. If the issue is something else, please elaborate a bit more. There are also existing planet gravity systems available to study if you’re up for that.


#13

I’ll get back to ya on that in a bit, taking a break from studio for right now, used it for quite a while and want to relax.


#14

I’m pretty sure I know what his issue is (I had the same yesterday), but I don’t have a fix. He wants the player’s bottom (hehehe) to face the planet, but with a bodygyro it makes the player’s front face the planet instead. Is this right, @dezway?


#15

Yeah essentially, except I want the front to face away from the planet.

Edit: Oh jeez I’ve never seen italics that strong.


#16

So that the player lays on it’s back? sorry i’m stupid


#17

No you’re right in the original comment, I must have misread.


#18


So, this is how your situation is currently? - Facing front towards the planet’s center.


And this is your goal? - Facing front away from the planet’s center.

Or rather this last option:


#19

I did this a little while ago for one of my space games. You may want to look into using a LineForce constraint. To use it you simply add an attachment to the two parts you are going to apply the force on. Set these attachments as Attachment0 and Attachment1 on the Lineforce. Then set the magnitude of the force. Since you say it is for gravity then you can set InverseSquareLaw to true and the magnitude will be set automatically. You can also set a maximum force. Finally, if you don’t want the force to affect the planet but still want to do things like rotate the planet while having it unanchored, you can set ReactionForceEnabled to false. The LineForce automatically updates the direction when the parts move.


#20

Last option.