Go to the moon
Here is how to build a small space game in Unity.
You must launch the rocket to the moon without hitting space rocks.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; [Range(0, 1)] public float smoothSpeed = 0.125f; public Vector3 offset; void FixedUpdate() { if (target != null) { Vector3 desiredPosition = new Vector3(transform.position.x, target.position.y, transform.position.z) + 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | using UnityEngine; using UnityEngine.SceneManagement; public class RocketRun : MonoBehaviour { private Rigidbody rg; [SerializeField] private float force, speed; private AudioSource rocketAudio; public GameObject rocketParticle; public GameObject rocketlight; public string nextLevel, startLevel; public GameObject ExplosionEffect, finishSound; string state = "Play"; private void Awake() { rg = GetComponent<Rigidbody>(); rocketAudio = GetComponent<AudioSource>(); } private void Update() { if (state == "Play") { MoveLRRocket(); RocketSoundAndParticel(); } else { Stop(); } } private void FixedUpdate() { if (state == "Play") MoveUpRocket(); } private void OnCollisionEnter(Collision collision) { if (state == "Play") { switch (collision.gameObject.tag) { case "Dead": Debug.Log("Dead"); state = "dead"; Dead(); break; case "Finish": Debug.Log("Finish"); state = "finish"; Instantiate(finishSound); Invoke("RunNextLevel", 3f); break; } } } void MoveUpRocket() { if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { rg.AddRelativeForce(Vector3.forward * force * Time.fixedDeltaTime); } } void MoveLRRocket() { if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { transform.Rotate(-Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { transform.Rotate(Vector3.up * speed * Time.deltaTime); } } void RocketSoundAndParticel() { if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) { rocketAudio.Play(); rocketParticle.SetActive(true); rocketlight.SetActive(true); } if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow)) { rocketAudio.Stop(); rocketParticle.SetActive(false); rocketlight.SetActive(false); } } void Stop() { rocketAudio.Stop(); rocketParticle.SetActive(false); rocketlight.SetActive(false); } void RunNextLevel() { SceneManager.LoadScene(nextLevel); } void Dead() { Instantiate(ExplosionEffect, transform.position - new Vector3(0, 0, 1), transform.rotation); gameObject.GetComponent<MeshRenderer>().enabled = false; Invoke("RunStartLevel", 2f); } void RunStartLevel() { SceneManager.LoadScene(startLevel); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using UnityEngine; public class RockTranslate : MonoBehaviour { [SerializeField] [Range(0, 10)] float speed; // Update is called once per frame void Update() { transform.Translate(new Vector3(1, 0, 0) * speed * Time.deltaTime); if (transform.position.x < -9 || transform.position.x > 9) speed = -speed; } } |
1 2 3 4 5 6 7 8 9 10 11 12 | using UnityEngine; public class Rotate : MonoBehaviour { public Vector3 ro; void Update() { transform.Rotate(ro * Time.deltaTime); } } |