Advertisement
StefanBashkir

Custom Camera Movement (Top-Down SIM Game)

Feb 27th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. local Camera = workspace.CurrentCamera
  2. local RunService = game:GetService("RunService")
  3. local UserInputService = game:GetService("UserInputService")
  4. local Player = game.Players.LocalPlayer
  5. local Mouse = Player:GetMouse();
  6. local Character = Player.Character or Player.CharacterAdded:wait()
  7.  
  8. -- Settings
  9. local MinCamGroundDistance = 20;
  10. local MaxCamGroundDistance = 70;
  11. local KeysHeld = {};
  12. local CamMovementTimeFactor = 1;
  13.  
  14. -- Wait for the entire character to load.
  15. while (Character.Parent ~= workspace) do
  16.     wait(0)
  17. end
  18.  
  19. Character:WaitForChild("Head")
  20. Character:WaitForChild("Left Arm")
  21. Character:WaitForChild("Right Arm")
  22. Character:WaitForChild("Torso")
  23. Character:WaitForChild("Humanoid")
  24. Character:WaitForChild("Right Leg")
  25. Character:WaitForChild("Left Leg")
  26.  
  27. Character:Destroy();
  28. Camera.CameraType = Enum.CameraType.Scriptable
  29.  
  30. function DefaultRotation()
  31.     local Position = Camera.CoordinateFrame.p
  32.     local RayCheck = Ray.new(Position, Vector3.new(0,-1,0) * 300);
  33.     local HitPart, HitPosition = workspace:FindPartOnRay(RayCheck)
  34.     if (HitPart) then
  35.         local DistanceToGround = (HitPosition - Position).magnitude
  36.         if (DistanceToGround < MinCamGroundDistance) then
  37.             Position = Position + Vector3.new(0, MinCamGroundDistance - DistanceToGround,0);
  38.         elseif (DistanceToGround > MaxCamGroundDistance) then
  39.             Position = Position - Vector3.new(0, DistanceToGround - MaxCamGroundDistance,0);
  40.         end
  41.     end
  42.     Camera.CoordinateFrame = CFrame.new(Position) * CFrame.fromAxisAngle( Vector3.new(1,0,0), -math.pi/4)
  43. end
  44.  
  45. DefaultRotation();
  46.  
  47. Mouse.KeyDown:connect(function(Key)
  48.     KeysHeld[Key:byte()] = true;
  49.     print(Key:byte());
  50. end)
  51.  
  52. Mouse.KeyUp:connect(function(Key)
  53.     KeysHeld[Key:byte()] = false;
  54. end)
  55.  
  56. RunService.RenderStepped:connect(function()
  57.     local CamAdjustment = Vector3.new(0,0,0);
  58.    
  59.     if (KeysHeld[119]) then
  60.         -- 'w' is held
  61.         local Forward = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(0,0,-1));
  62.         local AdjustedVector = Vector3.new(Forward.X,0,Forward.Z);
  63.         CamAdjustment = CamAdjustment + AdjustedVector
  64.     end
  65.    
  66.     if (KeysHeld[97]) then
  67.         -- 'a' is held
  68.         local Left = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(-1,0,0));
  69.         local AdjustedVector = Vector3.new(Left.X,0,Left.Z);
  70.         CamAdjustment = CamAdjustment + AdjustedVector
  71.     end
  72.    
  73.     if (KeysHeld[115]) then
  74.         -- 's' is held
  75.         local Backward = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(0,0,1));
  76.         local AdjustedVector = Vector3.new(Backward.X,0,Backward.Z);
  77.         CamAdjustment = CamAdjustment + AdjustedVector
  78.     end
  79.    
  80.     if (KeysHeld[100]) then
  81.         -- 'd' is held
  82.         local Right = Camera.CoordinateFrame:vectorToWorldSpace(Vector3.new(1,0,0));
  83.         local AdjustedVector = Vector3.new(Right.X,0,Right.Z);
  84.         CamAdjustment = CamAdjustment + AdjustedVector
  85.     end
  86.    
  87.     if (KeysHeld[113]) then
  88.         -- 'q' is held
  89.         Camera.CoordinateFrame = Camera.CoordinateFrame * CFrame.fromAxisAngle(Vector3.new(0,-1,0), math.rad(1))
  90.     end
  91.    
  92.     if (KeysHeld[101]) then
  93.         -- 'e' is held
  94.         Camera.CoordinateFrame = Camera.CoordinateFrame * CFrame.fromAxisAngle(Vector3.new(0,1,0), math.rad(1))
  95.     end
  96.    
  97.     Camera.CoordinateFrame = Camera.CoordinateFrame + CamAdjustment
  98. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement