Skip to main content

Creating Rotator and Pin Object

Operating System


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.

  1. Drag the sprite to the Scene View.
  2. Rename the sprite as Target.
  3. Set the component values as shown in the image
    tut28
  4. Add a component Circle Collider 2D and check the Is Trigger.
  5. Go to the Scripts folder and create a new folder inside of it called Gameplay.
  6. Create a C# script called Target.
  7. Write these block of code inside the Target Class:
    1. public float speed;
    2.  
    3. // Use this for initialization
    4. void Start () {
    5.  
    6. }
    7.  
    8. // Update is called once per frame
    9. void Update () {
    10. RotateMovement ();
    11. }
    12.  
    13. void RotateMovement(){
    14. transform.Rotate (0, 0, speed * Time.deltaTime);
    15. }
  8. Drag the script to Target inspector as a component, and set it speed to 100 to start rotate the GameObject.
  9. 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.
tut29

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.
tut30

Then add a UI Text object then set each component as shown below.

Pin Count UI

tut31

Pin Counter

32

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:

  1. using UnityEngine.UI;
  2. using UnityEngine.SceneManagement;

Write these blocks of code inside the Pin class:

  1. public float speed;
  2. public Rigidbody2D myRigidBody;
  3. public RuntimeAnimatorController cameraZoom;
  4. public bool isPrepareToShoot = false;
  5. public bool hasMove = false;
  6. public AudioClip hit;
  7.  
  8. private AudioSource audioSource;
  9. private Animator animator;
  10. private bool isSticked = false;
  11. private bool isFire = false;
  12. private bool hasTouched = false;
  13.  
  14. void Awake(){
  15. audioSource = GetComponent<AudioSource> ();
  16. animator = GameObject.FindGameObjectWithTag ("MainCamera").transform.GetComponent<Animator> ();
  17. }
  18.  
  19. // Use this for initialization
  20. void Start () {
  21.  
  22. }
  23.  
  24. // Update is called once per frame
  25. void Update () {
  26. if(Application.platform == RuntimePlatform.WindowsEditor){
  27. KeyBoardButton ();
  28. }else if(Application.platform == RuntimePlatform.Android){
  29. TouchButton ();
  30. }
  31.  
  32. MoveUpward ();
  33. }
  34.  
  35.  
  36. void KeyBoardButton(){
  37. if(Input.GetKeyDown(KeyCode.Space)){
  38. if(isPrepareToShoot){
  39. isFire = true;
  40. hasMove = true;
  41. }
  42. }
  43. }
  44.  
  45. void TouchButton(){
  46. if(Input.touchCount > 0){
  47. Touch touch = Input.GetTouch (0);
  48.  
  49. if(touch.phase == TouchPhase.Began){
  50. if(isPrepareToShoot){
  51. isFire = true;
  52. hasMove = true;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. void MoveUpward(){
  59. if (!isSticked && isPrepareToShoot && isFire) {
  60. myRigidBody.MovePosition (myRigidBody.position + Vector2.up * speed * Time.deltaTime);
  61. transform.GetChild (0).gameObject.SetActive (true);
  62. }
  63. }
  64.  
  65. void OnTriggerEnter2D(Collider2D collider){
  66. if (collider.CompareTag ("Target")) {
  67. isSticked = true;
  68. if(isSticked && !hasTouched){
  69. audioSource.PlayOneShot (hit);
  70. GameplayController.instance.score++;
  71. if(GameplayController.instance.score == GameObject.FindGameObjectWithTag("Spawner").transform.GetComponent<Spawner>().totalPin){
  72. GameObject.Find ("Background").transform.GetComponent<Image> ().color = Color.green;
  73. GameObject.FindGameObjectWithTag ("Target").transform.GetComponent<Target> ().enabled = false;
  74. Vector3 temp = transform.GetChild (0).transform.localScale;
  75. temp = new Vector3 (temp.x, temp.y * temp.y, temp.z);
  76. GameObject[] pin = GameObject.FindGameObjectsWithTag ("Pin");
  77. foreach(GameObject newPin in pin){
  78. newPin.transform.GetChild (0).transform.localScale = temp;
  79. newPin.transform.GetChild (1).transform.GetChild (0).transform.gameObject.SetActive (false);
  80. newPin.transform.GetComponent<SpriteRenderer> ().enabled = false;
  81. }
  82. StartCoroutine (NextLevel ());
  83. isPrepareToShoot = false;
  84. isFire = false;
  85. }
  86. }
  87.  
  88. GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent<Spawner> ().IsPrepareToFire ();
  89. transform.SetParent (collider.transform);
  90.  
  91. } else if(collider.CompareTag("Pin")) {
  92. if (!isSticked) {
  93. hasTouched = true;
  94. GameObject.FindGameObjectWithTag ("Target").transform.GetComponent<Target> ().enabled = false;
  95. GameObject.Find ("Background").transform.GetComponent<Image> ().color = Color.red;
  96. StartCoroutine (RestartLevel ());
  97. animator.runtimeAnimatorController = cameraZoom;
  98. isPrepareToShoot = false;
  99. isFire = false;
  100. }
  101. }
  102. }
  103.  
  104. IEnumerator NextLevel(){
  105. yield return new WaitForSeconds (1f);
  106. SceneManager.LoadScene ("Success");
  107. }
  108.  
  109. IEnumerator RestartLevel(){
  110. yield return new WaitForSeconds (1f);
  111. SceneManager.LoadScene ("Failed");
  112. }

After that attach the script to the Pin component and set Sorting Layer as Pin then Order in Layer to 1.

Set the script values as shown below.
tut33

Then Prefab the GameObject by dragging the Pin to the Prefabs folder, after that delete the Pin in the Scene View because we will just need to spawn the GameObject via Prefabs.

Tags

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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.