Creating Rotator and Pin Object
Submitted by razormist on Thursday, November 30, 2017 - 22:34.
Creating Rotator
We will now create the object that always rotate and where we put the Pin on. To do that first locates the sprite inside the Sprites Directory.- Drag the sprite to the Scene View.
- Rename the sprite as Target.
- Set the component values as shown in the image
- Add a component Circle Collider 2D and check the Is Trigger.
- Go to the Scripts folder and create a new folder inside of it called Gameplay.
- Create a C# script called Target.
- Write these block of code inside the Target Class:
- public float speed;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- RotateMovement ();
- }
- void RotateMovement(){
- transform.Rotate (0, 0, speed * Time.deltaTime);
- }
- Drag the script to Target inspector as a component, and set it speed to 100 to start rotate the GameObject.
- Add a Sorting Layer: Target, Pin, CanvasLayer and Number, after that set the Sorting Layer to Target.
Creating Pin
Now that the target is already done we will now create the Pin that we use throw to get a score. To start with first duplicate the Target GameObject then remove the Target script and rename it as Pin. Set its component values as shown below. Add a Rigidbody2D then set its Body Type as Kinematic. And also add an AudioSource then uncheck the PlayOnAwake. Create a child GameObject of Pin then set its component as shown below. Then add a UI Text object then set each component as shown below.Pin Count UI
Pin Counter
Then we will now create the script that makes this GameObject to move to the Target location. First go to the Scripts folder then Gameplay folder, and create a C# script name Pin. Import these important modules to get started:- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public float speed;
- public Rigidbody2D myRigidBody;
- public RuntimeAnimatorController cameraZoom;
- public bool isPrepareToShoot = false;
- public bool hasMove = false;
- public AudioClip hit;
- private AudioSource audioSource;
- private Animator animator;
- private bool isSticked = false;
- private bool isFire = false;
- private bool hasTouched = false;
- void Awake(){
- audioSource = GetComponent<AudioSource> ();
- animator = GameObject.FindGameObjectWithTag ("MainCamera").transform.GetComponent<Animator> ();
- }
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- if(Application.platform == RuntimePlatform.WindowsEditor){
- KeyBoardButton ();
- }else if(Application.platform == RuntimePlatform.Android){
- TouchButton ();
- }
- MoveUpward ();
- }
- void KeyBoardButton(){
- if(Input.GetKeyDown(KeyCode.Space)){
- if(isPrepareToShoot){
- isFire = true;
- hasMove = true;
- }
- }
- }
- void TouchButton(){
- if(Input.touchCount > 0){
- Touch touch = Input.GetTouch (0);
- if(touch.phase == TouchPhase.Began){
- if(isPrepareToShoot){
- isFire = true;
- hasMove = true;
- }
- }
- }
- }
- void MoveUpward(){
- if (!isSticked && isPrepareToShoot && isFire) {
- myRigidBody.MovePosition (myRigidBody.position + Vector2.up * speed * Time.deltaTime);
- transform.GetChild (0).gameObject.SetActive (true);
- }
- }
- void OnTriggerEnter2D(Collider2D collider){
- if (collider.CompareTag ("Target")) {
- isSticked = true;
- if(isSticked && !hasTouched){
- audioSource.PlayOneShot (hit);
- GameplayController.instance.score++;
- if(GameplayController.instance.score == GameObject.FindGameObjectWithTag("Spawner").transform.GetComponent<Spawner>().totalPin){
- GameObject.Find ("Background").transform.GetComponent<Image> ().color = Color.green;
- GameObject.FindGameObjectWithTag ("Target").transform.GetComponent<Target> ().enabled = false;
- Vector3 temp = transform.GetChild (0).transform.localScale;
- GameObject[] pin = GameObject.FindGameObjectsWithTag ("Pin");
- foreach(GameObject newPin in pin){
- newPin.transform.GetChild (0).transform.localScale = temp;
- newPin.transform.GetChild (1).transform.GetChild (0).transform.gameObject.SetActive (false);
- newPin.transform.GetComponent<SpriteRenderer> ().enabled = false;
- }
- StartCoroutine (NextLevel ());
- isPrepareToShoot = false;
- isFire = false;
- }
- }
- GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent<Spawner> ().IsPrepareToFire ();
- transform.SetParent (collider.transform);
- } else if(collider.CompareTag("Pin")) {
- if (!isSticked) {
- hasTouched = true;
- GameObject.FindGameObjectWithTag ("Target").transform.GetComponent<Target> ().enabled = false;
- GameObject.Find ("Background").transform.GetComponent<Image> ().color = Color.red;
- StartCoroutine (RestartLevel ());
- animator.runtimeAnimatorController = cameraZoom;
- isPrepareToShoot = false;
- isFire = false;
- }
- }
- }
- IEnumerator NextLevel(){
- SceneManager.LoadScene ("Success");
- }
- IEnumerator RestartLevel(){
- SceneManager.LoadScene ("Failed");
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 78 views