Happy Sheep
This is a very small game to teach movement, jump and double jump.
Also, the character returns to a specific point, after hitting an obstacle.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; public float smoothSpeed = 0.125f; public Vector3 offset; void FixedUpdate () { Vector3 desiredPosition = target.position + offset; Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position = smoothedPosition; //transform.LookAt(target); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | using UnityEngine; using System.Collections; public class Sheep_sc : MonoBehaviour { public float speed; public float jump; public AudioClip[] jumpAudioClip; AudioSource jumpAudio; public GameObject groundCheck; public float groundCheck_r; public LayerMask layerMask; bool ground; bool dudoubleJump; private void Start() { jumpAudio = gameObject.GetComponent<AudioSource>(); } // Update is called once per frame void Update () { if (!End.end) { ground = Physics2D.OverlapCircle(groundCheck.transform.position, groundCheck_r, layerMask); if (ground) { dudoubleJump = false; } if (Input.GetKeyDown(KeyCode.Space) && ground) { GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump); jumpAudio.clip = jumpAudioClip[0]; jumpAudio.Play(); } if (Input.GetKeyDown(KeyCode.Space) && !ground && !dudoubleJump) { GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump); dudoubleJump = true; jumpAudio.clip = jumpAudioClip[1]; jumpAudio.Play(); } if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y); gameObject.transform.rotation = new Quaternion(0, 0, 0, 0); } if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, GetComponent<Rigidbody2D>().velocity.y); gameObject.transform.rotation = new Quaternion(0, 180, 0, 0); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using UnityEngine; using System.Collections; public class kill_s : MonoBehaviour { public GameObject player; public GameObject checkPoint; void OnTriggerEnter2D(Collider2D col){ if (col.tag == "Player") { gameObject.GetComponent<AudioSource>().Play(); player.transform.position = checkPoint.transform.position; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using UnityEngine; public class End : MonoBehaviour { public GameObject winPanel; public AudioClip winAudioClip; public AudioSource winAudio; public static bool end = false; void OnTriggerEnter2D(Collider2D col) { if (col.tag == "Player") { end = true; winAudio.clip = winAudioClip; winAudio.Play(); winAudio.loop = false; Destroy(gameObject); winPanel.SetActive(true); } } } |