{"id":2187,"date":"2024-05-19T09:37:44","date_gmt":"2024-05-19T04:07:44","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=2187"},"modified":"2024-05-19T21:50:39","modified_gmt":"2024-05-19T16:20:39","slug":"setting-up-unity-firstpersoncontroller-in-terrain","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/setting-up-unity-firstpersoncontroller-in-terrain\/","title":{"rendered":"Setting-up Unity FirstPersonController in Terrain"},"content":{"rendered":"\n<p>Recently I attempted navigation of the player in Unity using its FirstPersonController component in a terrain environment. I had created character controller from scratch for the player navigation earlier, but wanted to use FirstPersonController because it makes development easy and has many features which can be controlled using configuration settings. And I wanted to do this in a terrain environment using Unity Terrain tools. <\/p>\n\n\n\n<p>I have documented my learnings and steps I took to implement the above in following sections and videos. The focus was more on \u201cHow To\u201d than creating a beautiful game scene. The final outcome looked like this &#8211; <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/21jFZ_ny24c?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Importing StarterAssets<\/h2>\n\n\n\n<p>Go to the Unity Asset Store and search for &#8220;Starter Assets\u201d for First Person. Get the asset pack, open it in Unity and import it from the package manager.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/ClusNAxSpMQ?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Install Terrain Tools<\/h2>\n\n\n\n<p>In the package manager, switch to Packages from \u201cUnity Registry\u201d and search for Terrain Tools. Install the terrain tools. At the end of the installation, it provides an option to \u201cDownload Asset Samples from Asset Store\u201d. Click on that button and get the assets from the Asset store. Open them in Unity and import from the package manager.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/I7KCl9lYGQc?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Import Foliage Tree Pack<\/h2>\n\n\n\n<p>I am using tree prefabs from Foliage Pack in this example. Get it from the Unity Asset Store and import it in your project<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/RMI_aqLs5KU?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Create Terrain<\/h2>\n\n\n\n<p>Create a terrain object in the scene. Create a new \u201cGround\u201d layer and set the new terrain created to this layer. Create a few hills\/mountains using \u201cRaise or Lower Terrain\u201d option in the \u201cPaint Terrain\u201d tab in the properties inspector. Then add terrain textures by selecting \u201cPaint Texture\u201d from the drop-down and adding multiple texture layers.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/DxaxPxiS_mU?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Add Trees<\/h2>\n\n\n\n<p>Switch to \u201cPaint Trees\u201d tab in the property inspector and add tree prefab. I selected pine2a prefab from Foliage Pack. &#8220;Paint&#8221; trees in the terrain either using brush at specific locations or \u201cMass Place Trees\u201d, which will add specified number of trees all over the terrain.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/mJeuUA6Sh-g?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Add Player<\/h2>\n\n\n\n<p>Add a player (Capsule) object and set up camera to follow the player.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/FG5G_TXAZfA?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Set First Person Controller<\/h2>\n\n\n\n<p>First add RigidBody component to the player. Then add FirstPersonController component . Here are the steps to configure this component.<\/p>\n\n\n\n<ul>\n<li>Add FirstPersonControllerScript from Assets-&gt;StarterAssets-&gt;FirstPersonController-&gt;Scripts to the player<\/li>\n\n\n\n<li>In the &#8220;First Person Controller&#8221; section of Player properties, go to Cinemachine section and drag and drop camera_root object from the Hierarchy to this field<\/li>\n\n\n\n<li>In the Ground layer mask field select only \u201cGround\u201d from the drop-down<\/li>\n\n\n\n<li>Set Player Input field to StarterAssets.inputactions<\/li>\n\n\n\n<li>From the Assets panel, drag and drop Assets-&gt;StarterAssets-&gt;InputSystem-&gt;StarterAssetsInput.cs on to Player Properties (i.e. add this script to the player object)<\/li>\n\n\n\n<li>Open properties for the Main Camera and add &#8220;Cinemachine Brain\u201d component to the camera<\/li>\n\n\n\n<li>Run the game and check if you are able to navigate the player using up\/down\/left\/right keys <\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/xxNfn4hBvwc?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Change Navigation<\/h2>\n\n\n\n<p>I wanted to change the default navigation. I did not want camera to look up\/down\/left\/right on mouse movement. Also I did not want the player to move sideways on pressing left\/right keys. Instead, I wanted player to turn\/rotate after pressing left\/right keys. To do this, open the configuration settings panel of \u201cPlayer Input\u201d by double-clicking \u201cStandardAssets\u201d value (we set this in an earlier step). Then change the configuration settings as explained in the video.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/ONP_SO19YwA?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Add Tree Collider<\/h2>\n\n\n\n<p>Trees we added from Foliage pack to the terrain either do not have collider or have mesh collider which does not work in terrain. Therefore you will see that player passes through trees instead of being blocked by them. <\/p>\n\n\n\n<p>To fix this, I created a new prefab from existing pine2a prefab. I added capsule collider to this new prefab and changed terrain settings to use this new prefab. After this change, player\u2019s movement is blocked by the trees.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/7QB6IlSlDKY?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Fix Player Jump<\/h2>\n\n\n\n<p>Jump action on the player does not work, because Ground Radius of First Person Controller component of the player is set to 0.5 and origin of the player is at approximately 2 meters height. The script for the First Person Controller checks if the player is on the ground by creating an invisible sphere around the player with radius specified in the above field and checks if any object (from the selected layers, in our case Ground layer) hit the sphere. If the player is not grounded, jump action does not take place. So, setting \u201cGround Radius\u201d to 3 meters fixes this issue.<\/p>\n\n\n\n<p>Another way to fix this issue is to use Character Controller\u2019s isGrounded method. Change GroundedCheck method in FirstPersonController.cs (in Assets-&gt;StarterAssets-&gt;FirstPersonController-&gt;Scripts folder) as below &#8211; <\/p>\n\n\n\n<p><code>private void GroundedCheck() {<br>    Grounded = _controller.isGrounded;<br>    return; <br>}<\/code><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/pqDO0NHnOqY?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Add Fog<\/h2>\n\n\n\n<p>In the last step I added light fog to the scene by changing Lighting properties. There is a check box to set fog in the Environment tab of Lighting properties. Check this box and adjust the fog density.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"525\" height=\"296\" src=\"https:\/\/www.youtube.com\/embed\/FQrFbRw1X9A?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<p><br><br>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I attempted navigation of the player in Unity using its FirstPersonController component in a terrain environment. I had created character controller from scratch for the player navigation earlier, but wanted to use FirstPersonController because it makes development easy and has many features which can be controlled using configuration settings. And I wanted to do &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/setting-up-unity-firstpersoncontroller-in-terrain\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting-up Unity FirstPersonController in Terrain&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[166],"tags":[164,165,163],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-zh","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2187"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=2187"}],"version-history":[{"count":15,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2187\/revisions"}],"predecessor-version":[{"id":2203,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2187\/revisions\/2203"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=2187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=2187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=2187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}