{"id":1270,"date":"2011-12-08T23:59:21","date_gmt":"2011-12-08T23:59:21","guid":{"rendered":"http:\/\/www.west-racing.com\/mf\/?page_id=1270"},"modified":"2011-12-11T19:35:26","modified_gmt":"2011-12-11T19:35:26","slug":"bouncing-ball","status":"publish","type":"page","link":"http:\/\/www.west-racing.com\/mf\/?page_id=1270","title":{"rendered":"Bouncing Ball"},"content":{"rendered":"<p><div class=\"message_box warning_box\">\n<p class=\"message_box_sign warning_box_sign\">Warning<\/p>\nStill being written\n<\/div><br \/>\nHow to use a stretch modifier to simulate a rubber bouncing ball.<\/p>\n<h3>BallBounce.cs<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]using UnityEngine;<\/p>\n<p>[ExecuteInEditMode]<br \/>\npublic class BallBounce : MonoBehaviour<br \/>\n{<br \/>\n\tpublic float ground\t\t= 0.0f;\t\t\/\/ Ground position<br \/>\n\tpublic float radius\t\t= 1.0f;\t\t\/\/ radius of the ball\/object<br \/>\n\tpublic float vel\t\t= 0.0f;\t\t\/\/ vertical velocity<br \/>\n\tpublic float spring\t\t= 10.0f;\t\/\/ spring rate of the object<br \/>\n\tpublic float py\t\t\t= 1.0f;\t\t\/\/ vertical position<br \/>\n\tpublic float mass\t\t= 1.0f;\t\t\/\/ objects mass<br \/>\n\tpublic float timescale\t= 1.0f;\t\t\/\/ time multiplier<\/p>\n<p>\tMegaStretch mod;<br \/>\n\tMegaModifyObject modobj;<\/p>\n<p>\t\/\/ Simple physics timsetp<br \/>\n\tvoid FixedUpdate()<br \/>\n\t{<br \/>\n\t\tfloat t = Time.fixedDeltaTime * timescale;<br \/>\n\t\tfloat fy = -9.81f * t;<\/p>\n<p>\t\tif ( py < ground )\n\t\t\tfy += (spring * (ground - py)) \/ mass;\n\n\t\tvel += fy * t;\n\t\tpy += vel * t;\n\t}\n\n\tvoid Update()\n\t{\n\t\t\/\/ Find the stretch mod\n\t\tif ( !mod )\n\t\t{\n\t\t\tmod = GetComponent<MegaStretch>();<br \/>\n\t\t\tmodobj = GetComponent<MegaModifyObject>();<br \/>\n\t\t}<\/p>\n<p>\t\tif ( mod )<br \/>\n\t\t{<br \/>\n\t\t\tVector3 pos = transform.position;<\/p>\n<p>\t\t\tfloat amt = py &#8211; ground;<\/p>\n<p>\t\t\tif ( amt > 0.0f )<br \/>\n\t\t\t\tamt = 0.0f;<\/p>\n<p>\t\t\tfloat y = py;<br \/>\n\t\t\tif ( y < ground )\n\t\t\t\ty = ground;\n\n\t\t\tif ( mod.amount == 0.0f &#038;&#038; amt == 0.0f )\n\t\t\t\tmodobj.enabled = false;\n\t\t\telse\n\t\t\t\tmodobj.enabled = true;\n\n\t\t\tmod.amount = (amt \/ radius);\n\t\t\tpos.y = y;\n\t\t\ttransform.position = pos;\n\t\t}\n\t}\n}[\/cc]\n\n\n\n<h3>Video Guide<\/h3>\n<p><a href=\"http:\/\/www.youtube.com\/watch?v=AJw4b0x4PME&#038;hd=1&#038;fs=1&#038;autoplay=1\" class=\"fancybox-youtube\"><img loading=\"lazy\" src=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytballbounce.jpg\" alt=\"\" title=\"ytballbounce\" width=\"680\" height=\"384\" class=\"aligncenter size-full wp-image-1431 fancy\" srcset=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytballbounce.jpg 680w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytballbounce-300x169.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use a stretch modifier to simulate a rubber bouncing ball. BallBounce.cs [cc lang=&#8221;c#&#8221;]using UnityEngine; [ExecuteInEditMode] public class BallBounce : MonoBehaviour { public float ground = 0.0f; \/\/ Ground [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":299,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/1270"}],"collection":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1270"}],"version-history":[{"count":25,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/1270\/revisions"}],"predecessor-version":[{"id":4624,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/1270\/revisions\/4624"}],"up":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/299"}],"wp:attachment":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}