![]() ![]() Then we create a camX and camY variable to control our camera movement. We then declare a few float variables for our xMin,xMax,yMax,yMin these will hold our min and max bounds from our boxcollider2d. First thing we added to our script is to get our mapBounds which is a boxcollider2d which have declared as public. This may seem very complicated when you first look at. using System.Collections ĬameraRatio = (xMax + camOrthsize) / 2.0f ĬamY = Mathf.Clamp(, yMin + camOrthsize, yMax - camOrthsize) ĬamX = Mathf.Clamp(, xMin + cameraRatio, xMax - cameraRatio) So let’s now add some c# code to our camera follow script and make it restrict the bounds on our camera. With the boxcollider2d added we now have a way of getting the bounds of our background scene. So go over to your bg or background game object and add in a boxcollider2d. You can do this on our background which we want to restrict our camera follow on. To create a camera bounds you firstly need to create a way of measuring bounds. Let us now implement some camera bounds so our player won’t go outside our background. If you run your player camera follow project now your camera should be following your player. Like so:Īlso drag your player into the follow transform slow like above. Go ahead now save this and add this to your camera. We then in fixedupdate change our camera transform position to that of our player only on the x and y axis because we are moving in 2d space. So simply put we declare a followTransform, this will be a our player’s transform we will be following with our camera. In it’s most basic form this is what you will need to make the camera follow your player with a script. Public class CameraFollow : MonoBehaviour I will explain how this works afterwards. Open that up in c# and use this script below. So go ahead and create a new c# script and call it camera follow like this. Unity 2d player follow and camera movement script ![]() So with this method we have very little control over our camera’s transform, it’s all dependent on the player transform.Ī better method is for us to rather use a script. So you will end up with something like this. So if you have done this and you have moved your player to the edge of the screen you have probably figured out that this is not the most efficient way.Īs your player may be able to go out of the bounds of the screen and so will your camera view. The simplest way to do a camera follow is to just child our camera to our player like this. ![]() Simplest player camera follow and movement So dragging that into our unity project we now have. I made this one in inkscape very quickly. Now you should have a moving player when you hit play in unity. Rb.velocity = new Vector2(-moveSpeed*ltaTime,0) Rb.velocity = new Vector2(0, moveSpeed * ltaTime) Rb.velocity = new Vector2(moveSpeed*ltaTime, 0) Rb.velocity = new Vector2(0, -moveSpeed * ltaTime) Start is called before the first frame update Public class PlayerMovement : MonoBehaviour Replace the code in that script with this. Now go ahead and open up the player movement script in visual studio. ![]() Once done let’s create a player movement script.ĭrag and drop that onto your player game object like so. Then change the rigidbod2d settings to be kinematic. Now go ahead and add a rigidbody2d component on the right by clicking on add component. We want to drag our sprite into our scene. So go ahead and right click in our assets folder and create a square sprite with the below steps in the screenshots. We will start off by adding a simple player as a square sprite. To start we create a new unity 2d project. We will then go ahead and add some bounds to our camera so we can limit our camera movement. From there we will put together a unity 2d camera movement which will allow for a top down player camera follow. With this we will just add a basic background so we get some depth in our 2d world. So I will setup a square as our player moving around left, right,up and down. First we need to setup a very basic unity 2d project with a player moving in our scene. ![]()
0 Comments
Leave a Reply. |