![]() With that, the camera should follow our player character nicely. Transform.position = targetPosition Vector3.Lerp(transform.position, targetPosition, smoothing * ltaTime) Public float smoothing =2f //player transform component Hence, we will use it to update our position and control the speed of the camera movement with a smoothing property: CameraController.cs using System.Collections This method interpolates between the camera’s current position and its target position every time it is called on Update(), creating a smoother transition when the camera moves. Instead of just setting the transform of the camera to the target position, we can smooth out its movement by using Vector3.Lerp(). With that, our camera follows our player at our desired offset.Īrticle continues after the advertisement: 5. Vector3 targetPosition = new Vector3(,, - offsetZ) Public float offsetZ = 5f //player transform component To achieve this in-game, we just need to calculate and apply that offset into our script: CameraController.cs using System.Collections The camera that we set up in the scene earlier by parenting it to the player had a lower z-coordinate relative to the player, so that it is always behind the player. Our camera is now right on top of our player. We will also need to offset it slightly backwards, so that the player falls into its field of vision. Now, our camera should be right on top of the player. Transform.position = playerPos.position targetPosition ![]() Vector3 targetPosition = new Vector3(,, ) //Set the position accordingly This can be done by creating a separate Vector3 that takes the x and z-coordinates of our player, while retaining the y-coordinates of our camera: CameraController.cs using System.Collections only follow the player on the x and z-axes). We want our camera to retain its position on the y-axis (i.e. The problem with this, however, is that this causes the camera to be snapped to the player GameObject’s pivot position. Start is called before the first frame update Hence, we set the camera’s position to the player’s position on Update() by writing a FollowPlayer() method and calling it: CameraController.cs using System.Collections PlayerPos = FindObjectOfType().transform įundamentally, we want the camera to follow the player about. Find the player gameobject in the scene and get its transform component Transform playerPos // Start is called before the first frame update Public class CameraController : MonoBehaviour Make a reference to the player’s transform attribute: CameraController.cs using System.Collections Setting up the camera scriptĬreate a new script named CameraController and attach it to the Camera GameObject. We recommend docking the Scene window elsewhere, so you will be able to preview the camera through the game window.Īrticle continues after the advertisement:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |