Calculate the orientation by direction

the tree falls incorrectly by the player’s direction

mouse.Button1Up:Connect(function()
 if mouse.Target then
  local TreeModel = mouse.Target.Parent.Parent :: Model

  local Size = TreeModel:GetExtentsSize() 
  local Pivot = TreeModel:GetPivot()

  local Bottom = Pivot + Vector3.yAxis * (-Size.Y/2)
  local Top = Pivot.Position + Vector3.yAxis * (Size.Y/2)

  local directionOfFail = (Vector3.new(Bottom.Position.X, HumRoot.Position.Y, Bottom.Position.Z) - HumRoot.Position).Unit
  local FallPos = (Bottom.Position + directionOfFail * Size.Y) - Top
  
  RayCast:Change(Enum.RaycastFilterType.Exclude, {TreeModel, workspace.Ray})
  local RayResult = RayCast:Cast(Top, FallPos.Unit*100)

  if RayResult then
   local angle = GetAngle(Bottom.Position, Top, RayResult.Position)

   local lastCF = Pivot :: CFrame
   local originToPartTransform = Bottom:ToObjectSpace(lastCF)
   
   for t = 0, 1, .01 do
    local X = angle * directionOfFail.Z * t
    local Z = angle * (directionOfFail.X*-1) * t

    Bottom = CFrame.new(Bottom.Position) * CFrame.Angles(X, 0, Z)
    lastCF = TreeModel:PivotTo(Bottom*originToPartTransform)
    task.wait()
   end
  end
 end
end)

I’m bad at math

3 Likes

Not sure whats going on in here currently. I’d do it as:
Calculate the direction (as you do, I see you’ve removed the Y difference)
Calculate the direction which is parallel to the ground and perpendicular to the direction
tipAxis = directionOfFail:Cross(Vector3.yAxis)
Tip the trunk by rotating the current CFrame by an angle around this axis (CFrame.fromAxisAngle iirc?)
You can continue using your raycast result, stop tipping when the distance between the treetop and the raycast-ed point is minimized, once it goes up, break the loop.

1 Like

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