{"id":1946,"date":"2013-07-31T14:34:28","date_gmt":"2013-07-31T13:34:28","guid":{"rendered":"http:\/\/www.holyfingers.co.uk\/main\/?p=1946"},"modified":"2015-03-23T11:54:56","modified_gmt":"2015-03-23T11:54:56","slug":"pointing-and-clicking","status":"publish","type":"post","link":"https:\/\/www.holyfingers.co.uk\/main\/blog\/2013\/07\/31\/pointing-and-clicking\/","title":{"rendered":"Pointing and Clicking&#8230;"},"content":{"rendered":"<p>As a few people have asked, I thought I&#8217;d share some notes about how I&#8217;m setting up my point-and-click interface with <a href=\"http:\/\/www.hutonggames.com\/\">Playmaker<\/a>. I&#8217;m pretty sure the Unity 4.2 update opened up NavMesh to Basic users but I don&#8217;t think the IK rig is accessible, I&#8217;m using that for its &#8220;look at&#8221; control so you&#8217;ll need Pro to recreate this setup completely. You&#8217;ll also need the <a title=\"Playmaker Add-ons\" href=\"https:\/\/hutonggames.fogbugz.com\/default.asp?W714\" target=\"_blank\">custom actions<\/a> for both Pathfinding and Mecanim&#8230;<\/p>\n<p>There are several things I wanted the character to do in response to a click from the user:<\/p>\n<ul style=\"text-align: justify;\">\n<li>look at where the click was made<\/li>\n<li>turn to face that point<\/li>\n<li>walk towards that point<\/li>\n<li>upon arrival, complete an action of some kind<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">It would also be nice to have a method for stopping a walk before completion and for clicked items to have their own pre-defined destinations eg. clicking on a mountain could result in walking to a balcony to look rather than all the way to the mountain&#8230;<\/p>\n<p style=\"text-align: justify;\"><!--more--><\/p>\n<p style=\"text-align: justify;\">ngg_shortcode_0_placeholder<\/p>\n<p style=\"text-align: justify;\">The character has Animator and Navmesh components, a simple walk cycle animation and clips for basic turning on the spot. The scene is set up like the above image, with several proxy objects to visualise and keep track of different variables:<\/p>\n<ul style=\"text-align: justify;\">\n<li><em><strong>Destination<\/strong>:<\/em> The Agent Destination variable is set to match this object every frame, if the two don&#8217;t match up the agent automatically repaths and moves towards it.<\/li>\n<li><em><strong>Target Destination<\/strong>:<\/em> This object is repositioned when the player clicks, having this separate from the <em>Destination<\/em> allows us to check and set some stuff before the agent sets off.<\/li>\n<li><em><strong>Look<\/strong>:<\/em> The character&#8217;s Look At variable is set to match this every frame<\/li>\n<li><em><strong>Look Axis<\/strong>:<\/em> This provides an off set pivot for the <em>Look<\/em> object, this helps to limit crazy looking head snapping if <em>Target-Look<\/em> happens to pass very close to the head. (You can see this issue in my previous <a href=\"http:\/\/www.youtube.com\/watch?v=5S3_mSOO0q8\" target=\"_blank\">video<\/a>)<\/li>\n<li><strong><em>Target-Look<\/em><\/strong> and <strong><em>LookOrig<\/em><em>:<\/em><\/strong> Provide positions for setting <em>Look.\u00a0<\/em><em>LookOrig<\/em> acts as an idle state to reset to.<\/li>\n<li><em><strong>Compass<\/strong>:<\/em> Is a roundabout way to ascertain the angle between whatever direction the character&#8217;s facing and the current target. After a click we rotate the <em>Compass<\/em> to look at the target, store it&#8217;s rotation as a variable and then reset it to zero.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">All the Global Variables we need to keep track every frame are updated in <em>Update_FSM<\/em> on an empty GameObject:<\/p>\n<ul style=\"text-align: justify;\">\n<li><em>Hero_SRT_Vector3<\/em><\/li>\n<li><em>Hero_Velocity_Float<\/em><\/li>\n<li><em>Target_Destination_Vector3<\/em><\/li>\n<li><em>Click_Type<\/em> (Int)<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">It also checks for a Right Mouse Click used to instantly stop the character walking. (The extra <em>Get Rotation<\/em> in there is for the camera and isn&#8217;t relevant to this setup&#8230;)<\/p>\n<p style=\"text-align: justify;\">ngg_shortcode_1_placeholder<\/p>\n<p>There&#8217;s a separate FSM on the <em>Look<\/em> object, this allows us to call <em>Update_Look<\/em> from\u00a0<em>Hero_FSM<\/em> and carry on without having to wait for it to finish.<\/p>\nngg_shortcode_2_placeholder\n<p style=\"text-align: justify;\">There are also simple FSMs on the <em>Floor<\/em> and the <em>Item,<\/em> both are pretty straight forward. For the <em>Floor<\/em> I&#8217;m using a mouse pick event to set <em>Target_Destination_Vector3<\/em> from a raycast, <em>Item<\/em> just sets a predetermined value. Both update <em>Click_Type<\/em> and then send the <em>Click<\/em> event to the <em>Hero FSM<\/em> where most of the work gets done.<\/p>\nngg_shortcode_3_placeholder\nngg_shortcode_4_placeholder\n<p style=\"text-align: justify;\">The Animator component uses three parameters to change between clips, two floats; <em>Velocity<\/em> and <em>Angle_to_Destination<\/em> and a <em>Turn<\/em> boolean.<\/p>\nngg_shortcode_5_placeholder\n<p style=\"text-align: justify;\">I&#8217;ve split the Hero FSM into 5 sections:<\/p>\nngg_shortcode_6_placeholder\n<p>&nbsp;<\/p>\n<ol>\n<li>In Idle we&#8217;re making sure the <em>Agent_Destination<\/em> is the same as the Hero&#8217;s current position and the Agent isn&#8217;t moving. If a click is detected we update the <em>Target_Destination<\/em> and send an Event to the <em>Look_FSM<\/em>, at this stage the character still doesn&#8217;t start walking, just looks.<\/li>\n<li>Compass rotation starts at zero as a child of the main character, <em>Compass<\/em> is rotated to face <em>Target_Destination<\/em> and its local rotation stored as <em>Angle_to_Target<\/em> before being reset to zero.<\/li>\n<li>The NavAgent automatically repaths if already in motion and small changes in direction don&#8217;t really require additional animation so, to avoid firing off the turning sequence, we check the current velocity and skip out a few states if the agent is already walking. An exception to this is if you click behind the character, in such a case the agent will stop and perform the turning sequence as if from standing.<\/li>\n<li>If required, we rotate towards the target, set the <em>Turn<\/em> boolean and finally update the <em>Destination<\/em> to start walking.<\/li>\n<li>Get remaining distance seems to require a slight pause in order to register, after that an Int Switch set by the clicked object differentiates between clicking on an item or the floor. Finally a state for handling post walk action, in this case I&#8217;m just setting off a little sprite animation.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/okLzdO6_PuU?rel=0\" width=\"648\" height=\"365\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>I&#8217;m sure there are loads of different ways of setting something like this up but this is where I&#8217;m at at the moment, crits and comments welcome&#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a few people have asked, I thought I&#8217;d share some notes about how I&#8217;m setting up my point-and-click interface with Playmaker. I&#8217;m pretty sure the Unity 4.2 update opened up NavMesh to Basic users but I don&#8217;t think the IK rig is accessible, I&#8217;m using that for its &#8220;look at&#8221; control so you&#8217;ll need &#8230; <a title=\"Pointing and Clicking&#8230;\" class=\"read-more\" href=\"https:\/\/www.holyfingers.co.uk\/main\/blog\/2013\/07\/31\/pointing-and-clicking\/\" aria-label=\"Read more about Pointing and Clicking&#8230;\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":2613,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[55,12],"tags":[43,44,63,75,58,76,57],"class_list":["post-1946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-game-development","category-tutorials","tag-3d","tag-animation","tag-character","tag-playmaker","tag-scripting","tag-toryanse","tag-unity"],"_links":{"self":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/1946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/comments?post=1946"}],"version-history":[{"count":122,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/1946\/revisions"}],"predecessor-version":[{"id":2615,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/posts\/1946\/revisions\/2615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/media\/2613"}],"wp:attachment":[{"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/media?parent=1946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/categories?post=1946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.holyfingers.co.uk\/main\/wp-json\/wp\/v2\/tags?post=1946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}