Jump to content

GetMousePosition().z ( Zoom ) Move Camera.

Recommended Posts

I have a problem, I wish that when moving the wheel of the mouse the camera gets closer or further away from the character.ย 

This I want to do with the mouse wheel, and is a kind of zoom, but I can not set things to work.ย 

I welcome any possible suggestions, and if possible a simple script code.ย 

	zoomCamara = ventana:GetMousePosition().z /500.0

	--zoomCamara ~= zoomCamara then
	-- Zoom / Se mueve el pivote de la cรกmara.
	if self.pivoteCamara:GetPosition(false).z < -2 then


Share this post

Link to post

This is my RTS project's mouse zoom code. I hope this c++ code can give you idea.

float cameraDistance = 15.0f;
float smoothness = 15.0f;
float zoomRangeMin = 8.0f;
float zoomRangeMax = 20.0f;;
Vec3 playerCameraPos;
Vec3 playerCameraNewPos;
float scrollSpeedFPS;
float mouseWheel;

int getMouseWheelDirection() {
	int wheelReturn = 0;
	float mouseWheelPos = Window::GetCurrent()->GetMousePosition().z;

	if (mouseWheel != mouseWheelPos) {
		if (mouseWheelPos < mouseWheel) {
			wheelReturn = 2; //Wheel Down
		else if (mouseWheelPos > mouseWheel) {
			wheelReturn = 1; //Wheel Up
		mouseWheel = mouseWheelPos;
	return wheelReturn;

void mouseMovement()
    playerCameraPos = playerCamera->GetPosition();
    playerCameraNewPos = playerCameraPos;
    scrollSpeedFPS = scrollSpeed * Time::GetSpeed();
    if (playerCameraNewPos.y != cameraDistance) {
		playerCameraNewPos.y = Math::Curve(cameraDistance, playerCameraNewPos.y, smoothness / Time::GetSpeed());

	int mouseWheelDirection = getMouseWheelDirection();
	if (mouseWheelDirection) {
		if (mouseWheelDirection == 1) {
			if (cameraDistance < zoomRangeMax) {
				cameraDistance = cameraDistance + 2;
		else if(mouseWheelDirection == 2){
			if (cameraDistance > zoomRangeMin) {
				cameraDistance = cameraDistance - 2;		



  • Confused 1

Share this post

Link to post
	if ventana:KeyHit(Key.F1) then
		zoom = 4
	elseif ventana:KeyHit(Key.F2) then
		zoom = 2 
	elseif ventana:KeyHit(Key.F3) then
		zoom = 0



It's the easiest thing I can do. I guess it's not that bad.

Share this post

Link to post

I haveย more simple one, only for detecting mouse scroll up/down, not for calculating HOW much scroll

in Main.lua, add this at the bottom of main while loop (right above "end")


lastMouseZ = window:GetMousePosition().z

and add this as a Global function

MouseScrollDirection = function()
  if window:GetMousePosition().z < lastMouseZ then return 1 --scolled up
  elseif window:GetMousePosition().z > lastMouseZ then return 2 --scrolled down
  elseif window:GetMousePosition().z == lastMouseZ then return 0 end --no scroll

Use this function in any Script:UpdateWorld() function to detect mouse scroll

function Script:UpdateWorld()
  if MouseScrollDirection == 1 then
    --do something with scroll up
    --example: camera:Move(0,0,1)
  if MouseScrollDirection == 2 then
    --do something with scroll down
    --example: camera:Move(0,0,-1)


  • Like 1

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...