generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Need help with 2D Platformer script.

Previous Thread :: Next Thread 
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
28 Sep 2017 07:04 AM
So i'm currently learning from the roblox wiki. I have so far gathered a novice understanding of the script and how it works.

I encountered a problem. When I test the script, it does work, when my character moves forward(to the right) the camera follows, but when the character moves backwards(to the left), the camera doesnt follow. My goal right now is to make the camera follow the player left and right. Here is the script, it is the same as in the wiki.

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 20
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')


local function setupCamera()
camera.CFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset), Vector3.new(0,cameraHeight,0))
end

setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
if player.Character and player.Character:FindFirstChild('HumanoidRootPart') then
local playerX = player.Character.HumanoidRootPart.Position.X
local cameraX = camera.CFrame.p.X

if cameraX - cameraXChase < playerX then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
end
end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)


Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
28 Sep 2017 07:09 AM
Bump, really need help with this. All helpful advise is appreciated.




Report Abuse
OAuth2 is not online. OAuth2
Joined: 27 Nov 2016
Total Posts: 751
28 Sep 2017 09:03 AM
I can't see the problem visually in my head. I'm guessing it has something to do with:
if cameraX - cameraXChase < playerX then
Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
28 Sep 2017 09:37 AM
^ I definitely think so. Thats probably it, idk how to fix it tho.


Report Abuse
GNUnotUNIX is not online. GNUnotUNIX
Joined: 05 Feb 2012
Total Posts: 15171
28 Sep 2017 11:31 AM
"local camera = game.Workspace.CurrentCamera"
Because ROBLOX is pretty poopy at times, often camera will become an useless camera, if you try manipulating it, it will do nothing.

Just redefine camera on every onUpdate()
Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
28 Sep 2017 01:03 PM
@GNUnotUNIX

Mind elaborating? Would appreciate it.


Report Abuse
GNUnotUNIX is not online. GNUnotUNIX
Joined: 05 Feb 2012
Total Posts: 15171
28 Sep 2017 01:05 PM
Elaboration on explaination:
ROBLOX is crap

Elaboration on script:
local function onUpdate()
camera = workspace.CurrentCamera -- this needs to be added
if player.Character and player.Character:FindFirstChild('HumanoidRootPart') then
local playerX = player.Character.HumanoidRootPart.Position.X
local cameraX = camera.CFrame.p.X

if cameraX - cameraXChase < playerX then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
end
end
end
Report Abuse
OAuth2 is not online. OAuth2
Joined: 27 Nov 2016
Total Posts: 751
28 Sep 2017 05:50 PM
^ Adding that line doesn't help..?
Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
29 Sep 2017 06:32 AM
Doesnt work, something else should be the problem.


Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
29 Sep 2017 06:35 AM
Bump,

if cameraX - cameraXChase < playerX then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)

I have a hard time visualising this, does anyone think this is the problem?


Report Abuse
Laedere is online. Laedere
Joined: 17 Jun 2013
Total Posts: 23601
29 Sep 2017 06:36 AM
Try this:

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 20
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')


local function setupCamera()
camera.CFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset), Vector3.new(0,cameraHeight,0))
end

setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
if player.Character and player.Character:FindFirstChild('HumanoidRootPart') then
local playerX = player.Character.HumanoidRootPart.Position.X
local cameraX = camera.CFrame.p.X

camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)


Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
29 Sep 2017 06:39 AM
@Laedere

I just tried that, the camera kept moving left regardless if I moved my character or not. This problem seems pretty difficult. Thanks tho


Report Abuse
Laedere is online. Laedere
Joined: 17 Jun 2013
Total Posts: 23601
29 Sep 2017 06:44 AM
Then try this, it will probably work.

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 20
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')


local function setupCamera()
camera.CFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset), Vector3.new(0,cameraHeight,0))
end

setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
if player.Character and player.Character:FindFirstChild('HumanoidRootPart') then
local playerX = player.Character.HumanoidRootPart.Position.X
local cameraX = camera.CFrame.p.X

if cameraX - cameraXChase < playerX then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
else
camera.CFrame = camera.CFrame - Vector3.new(cameraSpeed, 0, 0)
end
end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)


Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
29 Sep 2017 06:56 AM
@Laedere

Great! That actually worked, altho... now theres another problem. When my character is not moving, theres a sort of shaky vibration in the camera.


Report Abuse
TheFishToucher is not online. TheFishToucher
Joined: 07 Jul 2012
Total Posts: 7995
29 Sep 2017 07:23 AM
if cameraX - cameraXChase < playerX then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
elseif cameraX - cameraXChase > playerX then
camera.CFrame = camera.CFrame - Vector3.new(cameraSpeed, 0, 0)
end
Report Abuse
TheFishToucher is not online. TheFishToucher
Joined: 07 Jul 2012
Total Posts: 7995
29 Sep 2017 07:24 AM
if cameraX - cameraXChase - playerX ~= 0 then
camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, 0, 0)
end

probably smoother
Report Abuse
ElderRaven is not online. ElderRaven
Joined: 03 Jul 2017
Total Posts: 422
29 Sep 2017 07:27 AM
############### While Laedere's script fixed the problem, i'll be making another thread based on this new problem. Both the scripts didnt work, the second one made the camera move without my player moving. Appreciate it tho.
Report Abuse
ChrisPNG is not online. ChrisPNG
Joined: 28 Sep 2013
Total Posts: 147
30 Sep 2017 08:51 PM
Huh??
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image