Hey, so I have a cameraPart and the currentCamera's CFrame is set to the camerPart. I have this code that will make the cameraPart look at the mouse position;
local mousePos = mouse.Hit.Position
local result = CFrame.lookAt(cameraPart.Position, mousePos)
local t1 = tweenService:Create(cameraPart, TweenInfo.new(10), {CFrame = result })
t1:Play()
I want to add a way for the camera to stop rotating after a certain point. e.g when the player moves their mouse to the side of the screen the camera will rotate to it but eventually will stop and not keep rotating. I simplt want to add a way for the camera to stop rotating so it doesn’t rotate too far.
I will try to break this down to a simpler problem. Let’s say you have a number, and you only want it to range from -10 to 25. You know that the number is changing, and you want to keep it in that range. You can do this by having the following:
originalNumber = math.random(-100, 500) -- This is too big of a value, and your code can only accept numbers in a range of -10 to 25
rangedNumber = (originalNumber > 25 and 25) or (originalNumber < -10 and -10) or originalNumber
print(rangedNumber)
Now let’s say in your script that you would want to make the maximum cursor position 64 and the minimum 16 (Yes, I realize that it is a very small value but you can change the script to fit your game needs). You would need the following code:
mousePos = (mouse.Hit.Position > 64 and 64) or (mouse.Hit.Position < 16 and 16) or mouse.Hit.Position
But how does this work? All that is important is that you need to understand what and and or do, and what truthy, falsy values are.
and will always return the value of the second clause if both clauses are truthy. If either one of the clauses are not truthy, it will return false. or will return whichever clause is truthy, meaning that if the first clause is truthy, the second one won’t be checked. It will return the value of the first clause.
I won’t be getting too deep into truthy/falsy values, but basically falsy values are anything that is empty, such as 0, [], "", nil, and false. The rest of the values, such as 91, ["Foo", "Bar"], "Hello world!", and true will be truthy. You can read more about these here:
yes i realize that they are about javascript but it works practically the same in lua lmao
I hope this helped and that you learned something new today! and sorry for the essay lmao im not good at explaining stuff “briefly”
I dont think I full understand it this is the code I used
local mousePos = (mouse.Hit.Position > 64 and 64) or (mouse.Hit.Position < 16 and 16) or mouse.Hit.Position
local result = CFrame.lookAt(cameraPart.Position, mousePos)
local t1 = tweenService:Create(cameraPart, TweenInfo.new(10), {CFrame = result })
t1:Play()
but it didn’t work it gave me an error; attempt to compare number > to vector3
did I incorporate the code right?
First off, I will try to obtain the mouse’s position in another way, using UserInputService. This is because mouse.hit is a little bit different and UIS will give you the position on the screen.
local UserInputService = game:GetService("UserInputService")
local mousePosX = UserInputService:GetMouseLocation().X
local mousePosY = UserInputService:GetMouseLocation().Y
local MAX_DIST_X = 512
local MIN_DIST_X = 16
local MAX_DIST_Y = 512
local MIN_DIST_Y = 16
local regX = (mousePosX > MAX_DIST_X and MAX_DIST_X) or (mousePosX < MIN_DIST_X and MIN_DIST_X) or mousePosX
local regY = (mousePosY > MAX_DIST_Y and MAX_DIST_Y) or (mousePosY < MIN_DIST_Y and MIN_DIST_Y) or mousePosY
Then you will need to put the regX and regY values into something where you determine the camera’s location. Make sure not to do anything relatively, only use absolute values.
So would I add a simple if statement? For example if regX and regY then and would this restrict the camera from moving if the mouse moves to far? I want the camera to stop moving if it rotates to far kinda like this game.
local mousePos = mouse.Hit.Position
local result = CFrame.lookAt(cameraPart.Position, mousePos)
local t1 = tweenService:Create(cameraPart, TweenInfo.new(6), {CFrame = result})
t1:Play()
this is my current code;
local mousePosX = userInputService:GetMouseLocation().X
local mousePosY = userInputService:GetMouseLocation().Y
local MAX_DIST_X = 512
local MIN_DIST_X = 16
local MAX_DIST_Y = 512
local MIN_DIST_Y = 16
local regX = (mousePosX > MAX_DIST_X and MAX_DIST_X) or (mousePosX < MIN_DIST_X and MIN_DIST_X) or mousePosX
local regY = (mousePosY > MAX_DIST_Y and MAX_DIST_Y) or (mousePosY < MIN_DIST_Y and MIN_DIST_Y) or mousePosY
local t1 = tweenService:Create(cameraPart, TweenInfo.new(6), {})
t1:Play()
Well, the Z-Axis doesn’t really matter, which is what mouse.Hit.Position returns. So, if your Tween was working beforehand, then you should be all set.
local mousePosX = userInputService:GetMouseLocation().X
local mousePosY = userInputService:GetMouseLocation().Y
local MAX_DIST_X = 512
local MIN_DIST_X = 16
local MAX_DIST_Y = 512
local MIN_DIST_Y = 16
local regX = (mousePosX > MAX_DIST_X and MAX_DIST_X) or (mousePosX < MIN_DIST_X and MIN_DIST_X) or mousePosX
local regY = (mousePosY > MAX_DIST_Y and MAX_DIST_Y) or (mousePosY < MIN_DIST_Y and MIN_DIST_Y) or mousePosY
local mousePos = Vector3.new(regX, regY, 0)
local result = CFrame.lookAt(cameraPart.Position, mousePos)
local t1 = tweenService:Create(cameraPart, TweenInfo.new(6), {CFrame = result})
t1:Play()
Oh yeah and also remember to play around with these variables:
local MAX_DIST_X = 512
local MIN_DIST_X = 16
local MAX_DIST_Y = 512
local MIN_DIST_Y = 16
So that you can get the best results. If you want me to help you figure those values out, let me know.
Lol it is ok! I am always happy to teach. Anyways, just try it out with those variables and see if it restricts motion after a certain point. If it does, then we can move on to those variables.
local mousePosX = userInputService:GetMouseLocation().X
local mousePosY = userInputService:GetMouseLocation().Y
local MAX_DIST_X = 100
local MIN_DIST_X = 16
local MAX_DIST_Y = 100
local MIN_DIST_Y = 16
local regX = (mousePosX > MAX_DIST_X and MAX_DIST_X) or (mousePosX < MIN_DIST_X and MIN_DIST_X) or mousePosX
local regY = (mousePosY > MAX_DIST_Y and MAX_DIST_Y) or (mousePosY < MIN_DIST_Y and MIN_DIST_Y) or mousePosY
local mousePos = Vector3.new(regX, regY, 0)
local result = CFrame.lookAt(cameraPart.Position, mousePos)
local t1 = tweenService:Create(cameraPart, TweenInfo.new(1), {CFrame = result})
t1:Play()
what it did is just make my camera go to the side and I can hardly move it;