Question

Camera does become camera1. I want the camera to follow the player. Like bounce a little while following the player too even though the player is in view. Like rotating the part: local Camera = game.Workspace.Camera Camera.CameraType = Enum.CameraType.Custom local camera1 = game.Workspace.JVCameras:WaitForChild("camera1") local function scriptCamera() Camera.CameraType = Enum.CameraType.Watch Camera.CFrame = camera1.CFrame end scriptCamera()

Answers

Answered by GPT-4o mini
To create a camera that follows the player while adding a slight bouncing effect in Roblox, we can use a `RunService`'s `Heartbeat` event to continuously update the camera's position. The camera can be set to follow the player smoothly, while the bouncing effect can be achieved by using a sine function for vertical movement based on time.

Here's an example of how you might implement this in a LocalScript. This assumes that there is at least one player and that the camera's initial position is defined by `camera1`.

```lua
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Camera = game.Workspace.CurrentCamera -- Use CurrentCamera during runtime
local player = Players.LocalPlayer

local camera1 = workspace.JVCameras:WaitForChild("camera1")

-- Adjust these values to your liking
local bounceAmplitude = 0.5 -- Height of the bounce
local bounceFrequency = 2 -- How fast the bounce occurs

-- Function to update the camera's position
local function updateCamera()
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
-- Get the player's position
local playerPosition = player.Character.HumanoidRootPart.Position

-- Calculate the bouncing effect
local time = tick() * bounceFrequency
local bounceY = math.sin(time) * bounceAmplitude

-- Set the camera's CFrame
Camera.CFrame = CFrame.new(camera1.Position.X, camera1.Position.Y + bounceY, camera1.Position.Z) * CFrame.Angles(0, camera1.Rotation.Y, 0)
end
end

-- Connect the update function to the Heartbeat event
RunService.Heartbeat:Connect(updateCamera)

-- Initially set the camera type
Camera.CameraType = Enum.CameraType.Custom
```

### Explanation:

1. **Service References**: The script retrieves the `RunService` and the `Players` service to interact with the LocalPlayer.

2. **Initial Camera Setup**: It sets the camera to use `Enum.CameraType.Custom`.

3. **Bounce Logic**: The `bounceY` variable calculates the vertical position of the camera based on a sine wave function (`math.sin`). The `bounceAmplitude` determines how high the camera bounces, and `bounceFrequency` determines the speed of the bounce.

4. **Update Function**: The `updateCamera` function calculates the new camera position by adding the bounce effect to the Y coordinate of `camera1`.

5. **Heartbeat Connection**: The function is connected to the `RunService.Heartbeat` event, allowing it to update every frame smoothly.

### Usage:
- Place the LocalScript inside `StarterPlayerScripts` or any descendant so that it runs locally for the player.
- Adjust the `bounceAmplitude` and `bounceFrequency` to fit your desired effect.

With this script, the camera will follow the player while also bouncing slightly in a smooth manner.

Related Questions