Building a Camera Controller for a Strategy Game
Вставка
- Опубліковано 23 чер 2019
- In this video we take a look at how to build a camera system for a traditional simulation/strategy/management game. We look at how to rig a camera in Unity so that it moves and rotates around a center point in the world, rather than from a "first-person" perspective.
We also look at how to focus on and follow an object with the rig.
Be sure to LIKE and SUBSCRIBE if you enjoyed this guide! Share the video for extra love!
- - - - - - - - - - - - -
I've set up a Discord so viewers can hangout, chat and share their own game dev tips and tricks. It's also a perfect place to hear about what videos I'm working on, or discussing episodes in more detail with other members of the Game Dev Guide community.
You're invited to join the Discord at / discord
- - - - - - - - - - - - -
Socials:
Twitter - / gamedevguideyt
Facebook - / gamedevguideyt
Oh sweet. Really high quality tutorial. Very recommendable.
Wait did you use this for Islanders!?!
Hi jonas
@@verpix4956 Pretty sure he modified it using a similar strategy to the ones found on the asset store. It just polishes it up and adds a few extra features such as max and min zoom, but yes its very similar to this as far as I know.
If Jonas is here then I guess im set
Come on Jonas answear him..how did you make it in your game
For everyone who runs into problems with stuttering or jittering cameras here's a little advice that I wish was part of this video: Put ALL code that manipulates a cameras position or rotation in the LateUpdate() method not the Update()! This way you will ensure that nothing will move around after your cameras transform has already been updated during the frame.
Wow! Thank you! I thought all time that the problems is in the postprocessing. Great my problem is solved! ;)
it doesnt work
Also, if your using rigidbodies with Physics that the camera follows, put them in FixedUpdate so the Camera moves with the physics updates
thanks
This didn't help mine unfortunately
One thing we didn't cover here is how to limit the zoom and the pan of the camera so it won't go off world. Excellent stuff as always though.
Maybe it's not ideal but I ended up doing:
private void HandleZoom()
{
if (Input.mouseScrollDelta.y != 0)
{
newZoom += Input.mouseScrollDelta.y * zoomSpeed;
newZoom.y = Mathf.Clamp(newZoom.y, minZoom, maxZoom);
newZoom.z = Mathf.Clamp(newZoom.z, minZoom, maxZoom);
cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
}
}
@@betomaluje Clamping the positions is the ideal way :D
@@cankarkadev9281 Could you elaborate on maybe what that'd look like? Or is what Alberto did what you're referring to?
@@TheAwesomeAlan Clamp means clamping in Mathf.Clamp(), Clamp means that u "animate" a Cube from minZoom to maxZoom. I hope u understand Clamping now : )
I just did:
// Clamp newPosition to the bounds of the map (hardcoded -- not great, but can be extracted to variables at any point)
newPosition = new Vector3(Mathf.Clamp(newPosition.x, -7f, 7f), 0.1f, Mathf.Clamp(newPosition.z, -7f, 7f));
and in the zoom:
// Clamp zoom to prevent clipping into mesh or zooming too far out
newZoom = new Vector3(0, Mathf.Clamp(newZoom.y, 2, 12), Mathf.Clamp(newZoom.z, -12, -2));
Excellent video! The comparison between the old ortho vs perspective but low FOV was eye opening for me.
Right? I imagine it was just as eye opening as when I figured that out myself! 😂
The comments section is full of helpful tricks to make this better. Here is mine. If you use a logarithm on your zoom speed it will zoom farther the further away you get. Meaning the zoom will feel much more natural. It will also be a good idea to link your zoom into you panning speed. That way you can pan at quicker rate the further you get away negating the need for a faster pan speed. These are pretty complex functions that I'm still working on.
Very helpful tip!!
I will try this
Sorry, but how do I implement this?
@@jern2216 use an exponential function ie y=x^2 where y = pan speed and x = your caneras offset from focus. Depending on your scale you'll need to play around with the equation to get desired results.
Love the quality of this tutorial. Simple and straight forward but dives deep with all the features you would want in a Strategy Game camera. Cant wait to see more tutorials from you!
This is honestly one of the best unity tutorials I have ever seen!
Thanks very much! I've had a great idea for a sim like management game for some time but never did stop to figure out how to make this type of camera setup. Your method is much smoother than my attempts! :) Appreciate the video very much. Looking forward to the next one!
Dude what a high quality of a tutorial. Im so amazed. Huge respect u have a new fan and subscriber 😊
Interesting fact: the thing with the „fake orthographic view“ works in real life. If you use a tele photo lens (200mm+) you will loose nearly all perspective in the picture.
In a 3D Mockup Tool, i missed an orthographic view. So i just used a 5000mm camera lens and put it far, far away and here you go.
It just explained to me why this works and i found it really interesting. So i just randomly share these thoughts, maybe someone ist interested✌🏼
If you have problems with ROTATION not working (saw some in the comments):
- set the rigs position to null
- add 3D-Object > Cube to the rig to make its position visible
- then take the map / terrain / level and made its ground touch the cube
- now try to rotate again, it should work now
Explanation: many (including me at first) think the rig has to be above the terrain,
somewhere in the air. But it should be level with the ground. You will understand
the issue better once the rig is visible to you. Hope this helps, cheers.
I had this problem and your solution helped me fix it, thanks! The problem was it was rotating around the rig's position (what it's meant to do) but my rig was somewhere absolutely useless.
You can alternatively add a widget to the rig with the button beside where you name it in the inspector.
Thank you !
newRotation *= Quaternion.AngleAxis(rotationAmount, transform.up);
Nice tutorial, I found your video on a reddit post on unity 3d. I really liked the way you teach and your patience to explain everything. Also, the quality production is amazing. Keep going
Excellency! Can't describe how clear and well-rounded your tutorial is.
This... was a legit tutorial, thank you - I didn't attempt this yet but I am watching it again to practice. I had a plan today and watched a Unity camera tutorial which was done half assedly making assumptions about the viewers knowledge without providing references and spent nearly 6 hours researching to no avail. By comparison this was a breath of fresh air.
Thank you. This is exactly the information that I was looking for starting my new project. Will definitely look around your channel more for even more helpful guides
11:45 - Impressed by your commitments 🙏🏼
Was wanting to see mouse interactions.
Great work dude, exactly what i was looking for. Im currently moving from c++ to unity and it helps to have some nice tutorials to get my head round.
Thank you. Thank you so much. I am a long time gamer, script kitty programmer that finally has decided to give making content a shot. And in true self destructive fashion I have volunteered to make an RTS my first project. I am a glutton for punishment. Your camera set up is by for the most straight forward and descriptive means I have ever come across. This was an excellent video.
Love the isometric with depth trick! Thanks and great tutorial!
Actually useful tutorial. After hundreds of others, that handle perspective camera zoom as FOV change - this is truly a breath of fresh air. Top tier, sir, top tier. Love it.
This was fantastic! I really found this to be incredibly helpful. You do a great job in teaching and explaining all these details. Keep it up!
The BEST tutorial you can found for RTS camera
If you ever even think of making a Strategy game, You must watch this!
Amazing tutorial!
Thank you!
Thank you so much. I am new to Unity and am trying to create a game I have in my head. I’ve been having to figure out Isometric game design with its lack of tutorial and resources has been frustrating.
This camera setup will let me make the game in 3D where there is just much much more tutorials and resources for.
Your tutorials are absolutely amazing 😍 I am starting to prototype a new tower defense game and this will be a great rig for my camera. Thank you very much for this.
You could make the camera zoom and rotation a bit more intuitive (When looking at the gameobjects and values) by having another layer in between the camera and the rig called "Pivot", aligned with the camera. The camera would now only need to move on one axis to zoom, the "pivot" would handle rotation, and the "rig" would move across the landscape.
Very clear and well-produced tutorial! impressive.
Quite literary all you need to hear about camera movement in 1 simple to follow tutorial. Thanks for this! Like.... a lot!
well i've done it years ago..i was surprise many people don't use this trick..but this is nice tutorial delivery...i am subbing..
Brilliant video mate! Thank you for putting it together. Also, I enjoyed the humorous bits tossed in too. Subbed :)
Thanks for the wonderful lesson. UA-cam needs more Unity's awesome tutorial like this. I liked and subscribed.
I am an absolute noob at game dev, but I've made such a camera controller in my "game" with a plane, a sphere and four cubes, and it feels sooo satysfying :D THANKS!
Incredible that this tutorial is free. Well done, super useful!
Love love love your videos! Can't wait to see your rpg series! (If that's in the works)
I really want to do turn based combat properly with items, camera events, etc.
i know this video is very old.. but this was a beautiful tutorial.. well spoken, easy to follow and fantastic results A+
Thank you so much! I used this code in my Master Thesis and just now I implemented it in a project for my job. Much appreciated!!
this is a BEAUTIFUL piece of work.
Big thanks for this video! I've managed to create a solar system tactical overview using your video and applying it to the new Unity input system!
I love your sense of humor! :D Also great content as always on this channel.
Your tutorials are very high quality. Subbed
Great video as always. Best Unity videos on youtube by far !
Thanks so much!
Very easy to understand .great tutorial and great tutor.
Thank you, I really like the way you lectured this. It has helped me immensely.
Very useful guide! Thanks for sharing.
Wow thanks so much I learnt so many things from this short video very amazing and to tjw point with great explaining 💫
amazingly detailed and well explained tutorial!
Regarding "fast mode" : Draw inspiration from Supreme Commander and other strategy games that modify your speed by your zoom ( in a very organic way ). AKA, further out = faster, closer in = slower. This also helps to establish feelings of scale and give a very pleasing way of controlling your camera speed.
How do I do that?
Great video! Easy to implement and yet fully functional!!
This was an interesting tutorial and I like the concepts.
Since I had issues with the camera transform for my particular type of game, I ended up with a solution of zooming by changing the camera's FOV, as tweaking the FOV was something we already did in the video.
The function to handle zooming works like this, and requires a public/serialized reference to the camera.
I also made it so that holding the Shift key makes this zoom faster.
void HandleZoomInput() {
string axis = "Mouse ScrollWheel";
float zoomAmount = Input.GetKey(KeyCode.LeftShift)
? fastZoomAmount : defaultZoomAmount;
if (Input.GetAxis(axis) != 0) {
float val = Input.GetAxis(axis) * zoomAmount
* Time.deltaTime;
camera.fieldOfView -= val;
}
//Check if within bounds
if (camera.fieldOfView > minZoomAmount) {
camera.fieldOfView = minZoomAmount;
} else if (camera.fieldOfView < maxZoomAmount) {
camera.fieldOfView = maxZoomAmount;
}
}
There are probably better solutions to ensuring that the zoom stays within the bounds but this was one option for me.
Great job. Very smooth and easy
Really awesome tutorial ... clear, simple and great !!! thanks
Really helpful. So well explained. So well write. Thanks a lot!!
Looks great! Much better than most of the cameras out there. Totally using this in my western RTS sim now!
Wow...
Being born and raised with FPSes, the 'issue' during the first 2 minutes of the video would've never occurred to me lol...
I legitimately saw nothing wrong for the longest time...
But thinking about how I would walk-around and view a table-top game... it all clicks!
Bottom-line, excellent designing and scripting!
Absolutely amazing video, exactly the type of camera i was looking for. You should make a tycoon/simulation game asset for the store, I would buy it instantly :)
OMG this was perfect! you're an excellent tutor!
Awesome video. Definitely gonna use some of this. Now I just gotta convert it to using Touch!
Thank you for your excellent video. Now I can make a camera controller for my strategy game.
Thank you very Much, now I can start developing basic Mechanics based on this!
Great turorial! For anyone trying to clamp the zoom and finding the other examples below don't work, try this really simple one it works perfectly for me
public float maxZoom, minZoom; // I set these to -100 and -700 in inspector
(then in HandleMouseInput replace the scroll bit with below)
if(Input.mouseScrollDelta.y!=0)
{
if(Input.mouseScrollDelta.y>0 && newZoom.z < maxZoom)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
if (Input.mouseScrollDelta.y < 0 && newZoom.z > minZoom)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
}
It workied perfectly with the mouse indeed, but do you have any idea how to do it for keyboard?
@@thomascrozelon1648 I'm sure you figured this out by now, but all you have to do is add that extra && segment to the if statement (I used different variable names):
if(Input.GetKey(KeyCode.R) && newZoom.z < zoomInConstraint)
{
newZoom += zoomAmount;
}
if(Input.GetKey(KeyCode.F) && newZoom.z > zoomOutConstraint)
{
newZoom -= zoomAmount;
}
Idk why but it doesn't work for me
@@aurl3119 just use clamp
@@SaiponathGames How do you use clamp?
Thank you for sharing the top on Field of View
This was very helpful. One thing I did find is
that I had to multiply my "movementSpeed" by Time.deltaTime when it got changed by the keyboard. If I didn't then it would have difference speeds at different framerates. I tested it at 10, 30, 60, and 300 FPS using Application.targetFramerate
Thanks for this, it's exactly what I wanted for my game.
Nice. I spent 7 hours doing this on my own. I just couldn't be satisfied with what is default camera behaviour, so I revisited trigonometry and got going. I probably watched 2h of tutorials and read 50 posts. Noone did what I wanted.
Now I lay in bed at 1am and then stumble upon this...
Thanks for this great video, it contains some good tips.
Thank you. This is what I was looking for
so good, very nice, thanks!
you helped me make a touch controller with the same mechanics idea thanks a lot!.
Such a great video. So useful
btw, to anyone wanting a zoom with the scroll wheel, literaly just do this.
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
newZoom += zoomAmount;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
newZoom -= zoomAmount;
}
that was the best tutorial I ever watched👍
Nice I was looking some of these tricks.
Very cool video mate, honestly you make some really informative stuff!
Just wondering, do you think you could do a video on creating a Camera Controller but for a 3rd Person 3D-Platformer? Would love to see one of those!
Thank you very much! Now I can create a game with a good movement.
thank you so much! great video! please keep making another helpfull tutorial
Thank you for this great tutorial :
PERFECT tutorial!
Great video. Thank you
Very helpful thanks!
Excellent video!
Very excellent tutorial keep up the good work. Maybe to keep things in spirit of strategy games, you can make a tutorial next on minimap/fog of war :)
You are a hero, Sir.
Great video really!!
So dope tutorial !
That was an excellent tutorial!! Thank you very much. Any thoughts on animating the cars / planes? Thanks again, Greg
This is amazing, I just wish I could easily do that with Cinemachine...
Great video, very helpful. Wondererd if you'd consider updating it to the new Input System?
great video m8, very usefull
Brilliant video!
One think that I wanted in my game is to have a smooth transition when you start following/focusing an game object.
Hint for anyone needing the same:
You can easily do that by setting the newPosition variable in HandleMovementInput() method to be equal to followTransform.position, like that:
if (Input.GetKey(KeyCode.F)) // I removed the F binding from zoom because I allow zoom only via mouse.
{
newPosition = followTransform.position;
}
hello, i tried doing what you said as it is a cool feature but it doesn't seem to work for me (maybe i'm too much of a beginner...), so could you explain a bit more please ? :D
Nice video! Loved it!
But I have one question. How do I make a boundary around the scene, that the camera will stay inside? - Mostly just so the player doesn't move way out of the game.
brilliant, helped so much, thanks a lot ;-)
8:35 - The subtle big difference 😍😀 Wow.
You helped me a LOT
Great video, thanks :)
I really love you guy!
Thank you very much !!!
Great tutorial. I am wondering how one would want to place bounds on the zoom in this case.
very good tutorial
我找到了好久,终于找到了,非常感谢
Thankyou, So much.