{"id":70,"date":"2022-04-15T02:04:53","date_gmt":"2022-04-15T02:04:53","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/?p=70"},"modified":"2022-04-15T02:04:53","modified_gmt":"2022-04-15T02:04:53","slug":"when-life-knocks-you-down","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/2022\/04\/15\/when-life-knocks-you-down\/","title":{"rendered":"When Life Knocks You Down&#8230;"},"content":{"rendered":"\n<h2 class=\"has-large-font-size wp-block-heading\">Ask why your developer let you topple over in the first place. <\/h2>\n\n\n\n<h2 class=\"has-large-font-size wp-block-heading\">In this post, we will look at how to keep your character upright and other worthwhile reflections on character movement in Unity.<\/h2>\n\n\n\n<p>Welcome back! How are things going on the project, you ask? Well, the team is currently neck deep in developing the tutorial room and working with on the global features of our escape room game.  Ted was focused on writing an intial script for player movement and camera panning, but ran into some issues and asked for an assist on getting some of the glitches worked out.<\/p>\n\n\n\n<p>Our design doesn&#8217;t use a physical player, but the camera perspective.  Our goal was to be able to rotate on the left and right arrow and go forward and backward based on the direction the camera is facing.  Additionally, we want the camera to be able to pan on the x and y axes. <\/p>\n\n\n\n<p>Issues to be addressed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Player Capsule Tumbling<\/strong> &#8211; since our player isn&#8217;t supposed to be a gymnast, we wanted it to be upright.<\/li><li><strong>Camera Clipping &#8211; <\/strong>on the edges of the room we would get flashes of the scene outside<\/li><li><strong>No Player Rotation &#8211;<\/strong> player moved on x and y axes instead of rotating on y axis and moving on z axis<\/li><li><strong>Global Direction for Player Movement &#8211; <\/strong>the player movement direction was aligned to the world and not to the perspective of the player<\/li><\/ul>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Keeping an Upright Character<\/h3>\n\n\n\n<p>The fix for our player gymnastics was very simple.  It just required checking a couple boxes in the &#8220;Constraints&#8221; section of the &#8220;Rigid Body&#8221; component of the capsule we used for the player.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"826\" height=\"636\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-15-1.png\" alt=\"\" class=\"wp-image-78\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-15-1.png 826w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-15-1-300x231.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-15-1-768x591.png 768w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/figure>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Camera Clipping<\/h3>\n\n\n\n<p>This was another very simple fix that involved reducing the &#8220;Clipping Planes&#8221; field value for the camera.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"640\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-16.png\" alt=\"\" class=\"wp-image-79\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-16.png 595w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5491\/files\/2022\/04\/Screenshot-16-279x300.png 279w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/figure>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\" style=\"font-style:normal;font-weight:500\">Player Rotation and Global Direction for Player Movement<\/h3>\n\n\n\n<p>This was slightly more complicated and involved attaching a script to our player capsule.  These functions are interrelated. The code we used in our update function is broken down below.<\/p>\n\n\n\n<p style=\"font-size:1.4rem\">Left and Right Rotation of Player<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>float horizontal = Input.GetAxis(\"Horizontal\") * \n    RotationSpeed;\ntransform.Rotate(0, horizontal * Time.deltaTime, 0);<\/code><\/pre>\n\n\n\n<p><strong>Input.GetAxis<\/strong> checks for a particular input and in this case, if the left or right keys pressed. The keys associated with Horizontal and other arguments can be found and changed in Edit &gt;&gt; Project Settings &gt;&gt; Input Manager &gt;&gt; Axes.<\/p>\n\n\n\n<p><strong>transform<\/strong> moves the object and in this case rotates it left and right based on the input.<\/p>\n\n\n\n<p><strong>Time.deltaTime<\/strong> makes sure the speed of the movement is the same regardless of hardware.<\/p>\n\n\n\n<p style=\"font-size:1.4rem\">Forward and Backward Movement Based on Rotation of Player<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Vector3 vertical = Vector3.zero;\nvertical.z = Input.GetAxis(\"Vertical\") * MovementSpeed;\nmoveValues = transform.TransformDirection(vertical);\ncharacterController.Move(moveValues * Time.deltaTime);<\/code><\/pre>\n\n\n\n<p><strong>Input.GetAxis<\/strong> here checks for the forward and backward keys because that is what is currently associated with Vertical in the Input Manager.<\/p>\n\n\n\n<p><strong>transform<\/strong> is used here again with <strong>TransformDirection<\/strong>.  This changes the x, y, z axes to the local space which in this case is the player&#8217;s perspective from the world space or global space.<\/p>\n\n\n\n<p><strong>characterController.Move<\/strong> Moves the character based on a Vector3 passed as an argument.  In this case, it is just forward and backward since we have zeroed out the x and y directions. <\/p>\n\n\n\n<h2 class=\"has-large-font-size wp-block-heading\">(Camera) Perspective and Wrap Up<\/h2>\n\n\n\n<p>With the help of the sources listed below and some time, we were able to get the player movement sorted out.  Getting the camera to take the perspective of the player was as simple as making the camera a child of the player capsule and making sure the projection was on perspective. Integrating camera panning though, that&#8217;s a story for another time.<\/p>\n\n\n\n<p>Until the next post, be a goldfish.<\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\">Sources:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><a href=\"https:\/\/answers.unity.com\/questions\/1841902\/my-player-with-a-capsule-collider-is-tipping-over.html\">Keeping Character Upright<\/a><\/strong><\/li><li><strong><a href=\"https:\/\/www.youtube.com\/watch?v=LZgNLTWYCcQ\">Camera Clipping<\/a><\/strong><\/li><li><strong><a href=\"https:\/\/www.youtube.com\/embed\/e5g1nJcjz-M\">Player Movement<\/a><\/strong><\/li><li><strong><a href=\"https:\/\/forum.unity.com\/threads\/character-controller-move-local-vs-global-problem.107863\/\">Global to Local Direction<\/a><\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ask why your developer let you topple over in the first place. In this post, we will look at how to keep your character upright and other worthwhile reflections on character movement in Unity.<\/p>\n","protected":false},"author":12319,"featured_media":75,"comment_status":"closed","ping_status":"open","sticky":false,"template":"single-no-separators","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16,13],"class_list":["post-70","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-character-movement-2","tag-unity-game"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/users\/12319"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":11,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/posts\/70\/revisions\/85"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/media\/75"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/youhadmeatcode\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}