Yue Posted April 14, 2019 Posted April 14, 2019 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 self.pivoteCamara:Move(0,0,zoomCamara) end Quote
ErhanK Posted April 14, 2019 Posted April 14, 2019 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; } } } playerCamera->SetPosition(playerCameraNewPos); } 1 Quote
Yue Posted April 15, 2019 Author Posted April 15, 2019 if ventana:KeyHit(Key.F1) then zoom = 4 elseif ventana:KeyHit(Key.F2) then zoom = 2 elseif ventana:KeyHit(Key.F3) then zoom = 0 end self.PivoteCamara:SetPosition(0,0,zoom) self.PivoteCamara:Move(0,0,-5) It's the easiest thing I can do. I guess it's not that bad. Quote
tipforeveryone Posted May 6, 2019 Posted May 6, 2019 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 end 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) end if MouseScrollDirection == 2 then --do something with scroll down --example: camera:Move(0,0,-1) end end 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.