So I was working on adding a simple "floor" (basically code that keeps the camera from going below a certain level) and jumping when I encountered a bug that made my camera slowly look down, even though my code didn't have anything to do with camera.target. Some times will dip down to a y value of maybe 1.999... if anyone has a better solution than setting a depth limit, please let me know. Here is the code that deals with preventing the camera from falling, and jumping. if anyone needs all of the code, I will be happy to provide it.
if (IsKeyDown(KEY_SPACE)) {
if (camera.position.y <= 2.0f) {
yMomentum = yMomentum + speed;
}
}
// forces camera to be above 0
if (camera.position.y < 2.0f) {
camera.position.y = 2.0f;
yMomentum = 0;
}
edit. here is all the code if needed:
#include "raylib.h"
#include "rcamera.h"
#define MAX_COLUMNS 20
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3d world (position, target, up vector)
Camera camera = { 0 };
camera.position = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera position
camera.target = (Vector3){ 1.0f, 2.0f, 4.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 85.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
float yMomentum = 0.0f;
int cameraMode = CAMERA_FIRST_PERSON;
DisableCursor(); // Limit cursor to relative movement inside the window
void input(float xMomentum);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update camera computes movement internally depending on the camera mode
// Some default standard keyboard/mouse inputs are hardcoded to simplify use
// For advance camera controls, it's recommended to compute camera movement manually
UpdateCameraPro(&camera,
(Vector3) {
0.0f, // Move forward-backward
0.0f, // Move right-left
yMomentum // Move up-down
},
(Vector3) {
GetMouseDelta().x * 0.05f, // Rotation: yaw
GetMouseDelta().y * 0.05f, // Rotation: pitch
0.0f // Rotation: roll
},
0.0f); // Move to target (zoom)
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawPlane((Vector3) { 0.0f, 0.0f, 0.0f }, (Vector2) { 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
DrawCube((Vector3) { -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3) { 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3) { 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
EndMode3D();
// Draw info boxes
EndDrawing();
yMomentum = yMomentum - 0.001f;
// forces camera to be above 0
if (camera.position.y < 2.0f) {
camera.position.y = 2.0f;
yMomentum = 0;
}
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}