Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a part move to a specific side of another part?

Asked by 1 year ago

This is sort of hard to explain, let's say i want a part to teleport to the arm of the player, but on It's side, if i set the part's position to the player's arm position, the part will be inside the arm

Of course i could move it a little to the side, that way it would be on the surface of the arm, yet this is inconsistent because if the character is bigger, the arm would be bigger, and the part would stop to be on the surface of the arm again.

Is there something i can do to detect the surface position of a part?

I hope i explained myself correctly, i wish i could provide a visual example to better explain myself but I can't post pictures.

0
It's time for you to learn cframing greatneil80 2647 — 1y
0
Yea, i have a basic understanding of cframing, but mastering it would help me alot! SharkRayMaster 265 — 1y

2 answers

Log in to vote
1
Answered by
enes223 327 Moderation Voter
1 year ago

You can use CFrames because they contain rotation data so it is easier to move parts relative to each other. Just use the axis you want to choose the side, you know. I'm just going to give a script that you can change axis easily, I hope it isn't too advanced for you. Example:

local part1
local part2

local axis = "X" -- Use "X", "Y", or "Z"
local reverse = false -- reverse side
local offset = (part1.Size[axis] / 2) + (part2.Size[axis] / 2) * (reverse and -1 or 1)

part1.CFrame = part2.CFrame * CFrame.new(axis == "X" and offset or 0, axis == "Y" and offset or 0, axis == "Z" and offset or 0)
0
You used so many variables you kinda killed my brain lol, took me like 2 mins of re reading it to understand it, but with your help and the guy above's help, after messing around alot i got it to work, using CFrame instead of Position made it better FOR SOME REASON, but thanks. SharkRayMaster 265 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

The position of the top surface of a part will always be

Part.Position.Y + Part.Size/2
--To set it on the top part do

partToSet.Position.Y = otherPart.Position.Y + otherPart.Size.Y/2 + partToSet.Size.Y/2

try playing around with it to get your desired results

0
I'm getting an error code in the console saying "Y cannot be assigned to" , why is it? SharkRayMaster 265 — 1y
0
Took me some time of playing around, but i got it to work, thanks! SharkRayMaster 265 — 1y

Answer this question