Hi
I am trying to spawn an object 1.5 studs in front of a sphere, my code looks like this:
local shareMassPoint = game.ReplicatedStorage.SharedMass:Clone()
shareMassPoint.Parent = game.Workspace
shareMassPoint.BrickColor = ball.BrickColor
local pos = ball.Size.X/2 + dFromBall
shareMassPoint.CFrame = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * pos)
local goal = {}
local dis = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * pos)
goal.CFrame = dis * distance
First I set cloned object’s position to position of the ball + lookVector and away from the ball by size/2 + 1.5. And then I want to set the goal to this but + 5 studs, how would I do it?
Scottifly
(Scottifly)
November 30, 2021, 8:14pm
#2
Try
local dis = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X + 1.5 ,0,CameraLookVector.Z) * pos)
1 Like
Thanks, I tried it but I still get the error on line 23 (goal.CFrame = dis * distance), Vector 3 expected got number
Tom_atoes
(Tom_atoes)
November 30, 2021, 8:22pm
#4
You can add CFrames to each other. Using the *
operator you can subtract / add two CFrames.
Eg:
part1.CFrame = part2.CFrame * CFrame.new(5, 0, 0) -- positions part1 5 studs away from part2 on part2's local X axis
1 Like
Scottifly
(Scottifly)
November 30, 2021, 8:23pm
#5
Does dis or distance = 0? If so then 0 times a number is 0, which may affect your calculations.
1 Like
dis = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X + 1.5 ,0,CameraLookVector.Z) * pos)
distance = 5
dFromBall = 1.5
Thanks, I know about that but the problem is that I don’t know the axis. The axis is a combination of the position of the ball and the lookVector.
refer to this article for a better understanding of cframes:
tl;dr, use this code snippent:
local sphere = game.Workspace.Part --change this
local spawnedpart
--[[
change this to whatever you need for the clone part to be set. set your cloned part to the spawnedpart var tho!
--]]
spawnedpart.CFrame = sphere.CFrame * CFrame.new(1.5, 0, 0)
(i tested the code in studio btw)
1 Like
Thanks, but again it will change the value on 1 set axis. I want to edit my code to add it to the axis that is calculated with the position and the lookVector. To be more specific I want it to be like that:
ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * pos
The (* pos) part to be switched for * distance. I tried it in this part:
goal.CFrame = dis * distance
But it doesn’t work, I guess you have to make the code look something like CFrame.new(stuff here).
oh. well in that case, i will re-read the post and provide a follow-up, thanks for the clarification.
1 Like
I just tried doing this:
goal.CFrame = CFrame.new(Vector3.new(dis) * distance)
But it goes to 0,0,0, when I printed “Vector3.new(dis)” and “CFrame.new(Vector3.new(dis) * distance)” they both returned 0,0,0 for some reason?
And here is the whole script just for more info.
-- editable --
local distance = 5
local dFromBall = 1.5
local TweenService = game:GetService("TweenService")
local event = game.ReplicatedStorage.RemoteEvents.shareMassRequest
event.OnServerEvent:Connect(function(player, CameraLookVector)
local char = player.Character or player.CharacterAdded:Wait()
if char then
local ball = player.Character:FindFirstChild("Ball")
local ballMass = ball:FindFirstChild("Mass").Value
if ballMass > 10 then
local shareMassPoint = game.ReplicatedStorage.SharedMass:Clone()
shareMassPoint.Parent = game.Workspace
shareMassPoint.BrickColor = ball.BrickColor
local pos = ball.Size.X/2 + dFromBall
shareMassPoint.CFrame = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * pos)
local goal = {}
local dis = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X + 1.5 ,0,CameraLookVector.Z) * pos)
goal.CFrame = CFrame.new(Vector3.new(dis) * distance)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local tween = TweenService:Create(shareMassPoint, tweenInfo, goal)
tween:Play()
wait(15)
shareMassPoint:Destroy()
end
end
end)
is this how you intend for it to appear as? (pretend the decal presented is the lookvector)
if so: heres the code that i used:
local part = script.Parent
local clonedobject = game.Workspace.socialcredit
clonedobject.Anchored = true
clonedobject.CFrame = part.CFrame
local pos = part.Size.X/2 + 5
clonedobject.CFrame = part.CFrame * CFrame.new(pos + 1.5, 0, 0) * CFrame.Angles(0, math.rad(180), 0)
if not, reply with a follow-up
1 Like
It is not quite what I am looking for, I got the spawn position sorted out:
local pos = ball.Size.X/2 + dFromBall
shareMassPoint.CFrame = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * pos)
What I am trying to do now is get the goal for tweenSerivce, which should be just the clonedObject moved by 5 studs in the direction it got spawned so the calculation of combined ball position and lookVector (which I already have, it’s the code above)
This is what I tried to do:
local dis = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X + 1.5 ,0,CameraLookVector.Z) * pos)
goal.CFrame = CFrame.new(Vector3.new(dis) * distance)
dis: CFrame of the clonedObject
distance: 5
I tried moving it by 5 studs using CFrame towards the direction it got spawned in, but it returns 0,0,0.
(I am new to CFrame’s, I tried what I could but it didn’t work out)
Hope it gives more information of what I want to achieve.
123andcut
(Zzzzzzzzzz)
November 30, 2021, 9:20pm
#15
Sorry, not quite sure what you are trying to achieve. is it simply adding 5 more studs away?
goal.CFrame = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z) * (pos+distance))
1 Like
That’s exactly it, thank you so much! Works perfectly, I just had to play around a bit with:
local pos = ball.Size.X/2 + dFromBall
Because it seems to go a bit too far.