{"id":2461,"date":"2019-09-26T05:08:24","date_gmt":"2019-09-26T05:08:24","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=2461"},"modified":"2019-09-26T05:11:02","modified_gmt":"2019-09-26T05:11:02","slug":"clamp-rotation-of-camera-in-unity","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/clamp-rotation-of-camera-in-unity\/","title":{"rendered":"Clamp rotation of camera in Unity"},"content":{"rendered":"\n<p>Some notes and issues when trying to clamp the rotation of a camera in Unity.  All the code was put on a script assigned to a controller that controlled the camera view.  Used the Simple Touch Controller from the asset store.<\/p>\n\n\n<p><strong>Problem trying to clamp EulerAngles<\/strong><\/p>\n\n\n<p>It appears that the following code does not work as EulerAngles expects a value from 0-365.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>transform.localEulerAngles = new Vector3(Mathf.Clamp(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook, 0f , 20f ),transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook, 0f);<\/code><\/pre>\n\n\n\n<p><strong>Extra debug info<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Debug.Log(\"Rotation = \" + transform.localEulerAngles.ToString());<\/code><\/pre>\n\n\n\n<p><strong>Code to clamp rotation<\/strong><\/p>\n\n\n\n<p>You have to do some math to take the value from 0-365, then translate it to -90 &#8211; 90 so it can be clamped properly <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Put these with the rest of your public variables.  These should show up in the Unity Inspector and allow you to modify the values\npublic float speedProgressiveLook;\npublic float clampXRotationUp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/  The following lines allow the script to Clamp the camera X rotation and should go in the Update function<\/code>\n\n<code>float playerRotation = transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook; <\/code>\n\n<code>if (playerRotation > 180)        {             <\/code>\n    <code>playerRotation = playerRotation - 360 ;  \/\/ Sets our working numbers between -180 - 180  <\/code>\n<code>}    <\/code>\n<code>     <\/code>\n<code>playerRotation = Mathf.Clamp(playerRotation, clampXRotationDown, clampXRotationUp);<\/code>\n\n<code>Vector3 newRotation;<\/code>\n<code>newRotation.x = playerRotation;<\/code>\n<code>newRotation.y = transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook;<\/code>\n<code>newRotation.z = 0;<\/code>\n\n<code>transform.localEulerAngles = newRotation;<\/code><\/pre>\n\n\n\n<p>The following links were helpful.<\/p>\n\n\n\n<p><a href=\"https:\/\/gamedev.stackexchange.com\/questions\/120500\/using-mathf-clamp-in-vector3-to-restrict-object-drag\">https:\/\/gamedev.stackexchange.com\/questions\/120500\/using-mathf-clamp-in-vector3-to-restrict-object-drag<\/a><br><br><a href=\"https:\/\/www.reddit.com\/r\/Unity3D\/comments\/8ue9fp\/mathfclamp_is_refusing_to_work_can_anybody_offer\/\">https:\/\/www.reddit.com\/r\/Unity3D\/comments\/8ue9fp\/mathfclamp_is_refusing_to_work_can_anybody_offer\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/26682155\/using-mathf-clamp-in-unity-for-boundries\">https:\/\/stackoverflow.com\/questions\/26682155\/using-mathf-clamp-in-unity-for-boundries<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some notes and issues when trying to clamp the rotation of a camera in Unity. All the code was put on a script assigned to a controller that controlled the camera view. Used the Simple Touch Controller from the asset &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/clamp-rotation-of-camera-in-unity\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[82,644,606,326,327],"class_list":["post-2461","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-code","tag-programming","tag-unity","tag-unity3d"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=2461"}],"version-history":[{"count":14,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2461\/revisions"}],"predecessor-version":[{"id":2656,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2461\/revisions\/2656"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}