{"author_name":"TheComputerGamerYT","cat":"LD #31","comments":[],"epoch":1417879320,"likes":3,"metadata":{"p_key":"24011","p_author":"TheComputerGamerYT","p_authorkey":"503683","p_urlkey":"59413","p_title":"Gone quite well","p_cat":"LD #31","p_event":"LD31","p_time":"1417879320","p_likes":"3","p_comments":"0","p_status":"UPD5","us_key":"503683","us_name":"thecomputergameryt","us_username":"thecomputergameryt","event_start":"1417737600","event_key":"26","event_name":"LD31"},"text":"<p>Finally completed the character controller for the char. At the moment we have a black box but I hope to change that later. It reacts to gravity, can jump and move left and right. It took forever with endless bugs along the way for example when you use the velocity on a rigidbody2D in unity it completely overrides the inbuilt gravity so you have to put your own in. Never the less it is working and here is the code if anyone else wants to know what I did. Feel free to use some of it in your own code <img src=\"http:\/\/ludumdare.com\/compo\/wp-includes\/images\/smilies\/simple-smile.png\" alt=\":)\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> :<\/p>\n <p>&nbsp;<\/p>\n <p>using UnityEngine;<br \/>\n using System.Collections;<\/p>\n <p>public class CC : MonoBehaviour {<\/p>\n <p>\/\/Rigidbody and falling<br \/>\n private Rigidbody2D rigid;<br \/>\n public bool falling = false;<br \/>\n public bool jumping = false;<\/p>\n <p>\/\/Keys<br \/>\n public KeyCode jump = KeyCode.W;<br \/>\n public KeyCode left = KeyCode.A;<br \/>\n public KeyCode right = KeyCode.D;<\/p>\n <p>\/\/Speed at which to do things<br \/>\n public float walkSpeed = 6;<br \/>\n public float jumpSpeed = 5;<\/p>\n <p>\/\/Sorting out gravity<br \/>\n public Vector3 gravity;<br \/>\n public Vector2 gravityToAdd = new Vector2();<\/p>\n <p>\/\/Falling calculation variables<br \/>\n public Transform lineStart;<\/p>\n <p>\/\/Get the rigidbody from char and gravity ammount from physics<br \/>\n void Start () {<br \/>\n rigid = this.GetComponent&lt;Rigidbody2D&gt;();<br \/>\n gravity = Physics2D.gravity;<br \/>\n }<\/p>\n <p>\/\/Actually move stuff WHAT?!?!?!?!<br \/>\n void Update () {<\/p>\n <p>\/\/Check if we are still jumping<br \/>\n if (gravityToAdd.y &lt; 0) {<br \/>\n jumping = false;<br \/>\n }<\/p>\n <p>\/\/The force that we will move<br \/>\n Vector2 force = new Vector2();<\/p>\n <p>\/\/Walk left<br \/>\n if (Input.GetKey(left)) {<br \/>\n force.x = -walkSpeed;<br \/>\n }<\/p>\n <p>\/\/Walk right<br \/>\n if (Input.GetKey(right)) {<br \/>\n force.x = walkSpeed;<br \/>\n }<\/p>\n <p>\/\/Landing detection<br \/>\n if (!Physics2D.Linecast(new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (this.transform.position.x, this.transform.position.y &#8211; 1.22f), 1 &lt;&lt; LayerMask.NameToLayer(&#8220;Ground&#8221;))) {<br \/>\n falling = true;<br \/>\n } else {<br \/>\n falling = false;<br \/>\n }<\/p>\n <p>\/\/Show a line in the editor<br \/>\n Debug.DrawLine (lineStart.localPosition, new Vector3(this.transform.position.x , this.transform.position.y -1.22f, this.transform.position.z), Color.cyan);<br \/>\n \/\/If falling apply gravity if not gravity = 0<br \/>\n if (falling == false &amp;&amp; jumping != true) {<br \/>\n gravityToAdd.y = 0;<br \/>\n } else {<br \/>\n gravityToAdd.y += gravity.y * Time.deltaTime;<br \/>\n }<\/p>\n <p>\/\/Add jump force<br \/>\n if (Input.GetKeyDown (jump) &amp;&amp; falling == false) {<br \/>\n jumping = true;<br \/>\n gravityToAdd.y = jumpSpeed;<br \/>\n }<\/p>\n <p>\/\/Apply gravity to force<br \/>\n force.y = gravityToAdd.y;<br \/>\n rigid.velocity = force; \/\/Apply force to rigidbody<br \/>\n }<br \/>\n }<\/p>","time":"December 6th, 2014 10:22 am","title":"Gone quite well"}