I need help moving camera!
Blitz3D Forums/Blitz3D Programming/I need help moving camera!
| ||
I am currently making a FPS(First person game but not shooter!) with mouse looking. However, when mouse goes up to look upward direction and going foward at the same time, the character also goes up and flying. Here is the code, what do I have to fix? If KeyDown(dir\dir_up) And KeyDown(42) Then MoveEntity camera,0,0,0.2 EndIf If KeyDown(dir\dir_down) Then MoveEntity camera,0,0,-0.1 EndIf If KeyDown(dir\dir_down) And KeyDown(42) Then MoveEntity camera,0,0,-0.3 EndIf If KeyDown(dir\dir_right) Then MoveEntity camera,0.1,0,0 EndIf If KeyDown(dir\dir_right) And KeyDown(42) Then MoveEntity camera,0.3,0,0 EndIf If KeyDown(dir\dir_left) Then MoveEntity camera,-0.1,0,0 EndIf If KeyDown(dir\dir_left) And KeyDown(42) Then MoveEntity camera,-0.3,0,0 EndIf If KeyDown(dir_rotate_right) Then TurnEntity camera,0,-2,0 EndIf If KeyDown(dir_rotate_left) Then TurnEntity camera,0,2,0 EndIf |
| ||
The easiest way to do this is not to move the camera, but to move a pivot that is the parent of the camera. Have the left and right mouse movement yaw the pivot and it will move correctly in the 2D plane, more or less as the camera is now; have the up and down mouse movement pitch the camera as it was already, and you'll be able to look up and down without affecting the movement of the player, who controls that through the pivot. If you aren't familiar with entity parenting: a child entity is "pinned" to its parent's space - it can still move on its own, but when the parent moves it gets forcibly dragged along as well. So by attaching the camera to a pivot, you can still move the camera, because anything done to the pivot will happen to it too; but anything that happens only to the camera will not affect the pivot, and since movement is the pivot's responsibility, the camera pitching up and down won't be able to affect it. |
| ||
Function GameInput() If KeyDown(200) Then MoveEntity player,0,0,2 ;up EndIf If KeyDown(208) Then MoveEntity player,0,0,-2 ;down EndIf If KeyDown(203) Then camyaw#=2 TurnEntity player ,0,camyaw#,0 ;left EndIf If KeyDown(205) Then camyaw#=-2 TurnEntity player ,0,camyaw#,0 ;right EndIf x#=EntityX(camera) y#=EntityY(camera) z#=EntityZ(camera) x1#=EntityX(player) y1#=EntityY(player) z1#=EntityZ(player) terra_y#=TerrainY(terrain1,x#,y#,z#)+5 PositionEntity camera,x#,terra_y#+camy#,z# terra_y1#=TerrainY(terrain1,x1#,y1#,z1#) If Not EntityCollided(player,2) PositionEntity player,x1#,terra_y1#+4,z1# EndIf End Function the camera is entityparented to the player |
| ||
thank you for help guys it really helped |
| ||
Interesting post, I was looking for something along these lines, thanks. |
| ||
You may also use MouseXSpeed and MouseYSpeed for mouse-look. In that case I would suggest to use xspeed on the base pivot so it actually allows to steer, and yspeed on the camera only. You move the mouse to the center of the screen each frame and then check the speed. See also my chasecam example in the archive, tho it is third person perspective. Vwait S#=-mousexspeed()/5.0 Movemouse graphicswidth()/2,graphicsheight()/2 Something like that. Actually you may have to try other positions for vwait or flip true. |