Unity mini game 2D (see the seam)

Hits: 0

First, make a needle and a target. You can choose an arrow to shoot a target. You can find materials on the Internet. Note that the target is best to be round.
The image dragged into unity needs to be modified to the following properties

Create a target named Circle, a pin named Pin, a Text to calculate the score, and three empty objects named as follows:

InsPoint is where the needle is generated, StartPoint is where the game starts, and the distance between the two is farther. The x-axis of InsPoint, StartPoint, and Circle are the same.
1. Modify the properties of text to
change the width and height to 3, and change the font size Modify it to 1, adjust the appropriate size and adjust to the appropriate position.

  1. Hang the code on the generated target

public class RotateSelf : MonoBehaviour
{
    public  float speed = 30 ; //The speed of the ball's rotation 
    // Start is called before the first frame update 
    void  Start ()
     {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate( new Vector3( 0 , 0 ,-speed*Time .deltaTime));
         //This is clockwise rotation. If you want to rotate counterclockwise, delete the - sign before -speed
    }
}

This is the speed of the target representing the middle rotation

  1. Generate a needle, add Rigidbody 2D to the needle object, set the Gravity to 0, adjust the Capsule Collider 2D to an appropriate size, and modify the Pin’s Tag property to PinHead.
    Then hang the code on the pin

public class Pin : MonoBehaviour
{

    public  bool isReah= false ;
     public  bool isFly= false ;
     private Transform startPoint;
     private Transform circlePos; // position of the circle 
    public  float speed = 3 ; // speed of needle movement 
    private Vector3 endPoint; // end position 
    // Start is called before the first frame update 
    void  Start ()
     {
        startPoint = GameObject.Find("StartPoint").transform;
        circlePos=GameObject.Find("Circle").transform;
        endPoint = circlePos.position;
        endPoint.y -= 2.6f;
    }

    // Update is called once per frame
    void Update()
    {
        if (isFly == false)
        {
            if (isReah == false)
            {
                transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
                if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
                {
                    isReah = true;

                }
            }
        }
        else
        {
            transform.position = Vector3.MoveTowards(transform.position, endPoint, speed * Time.deltaTime);
            if (Vector3.Distance(transform.position, endPoint) < 0.05)
            {
                isFly = false;
                transform.parent = circlePos;
            }
        }
    }
    public void StartFly() {
        isFly = true;
        isReah = true;
    }
}

This is to end the game when it is detected that the needle touches another needle

public class PinHead : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "PinHead")
        {
            GameObject.Find( "GameManger" ).GetComponent<GameManger>().GameOver();
             //Game over
        }
    }

}

Then move the needle into the Perfab to generate a prefab, and delete the needle’s entity.

  1. Hang the code
    in GameManger Add two namespaces in GameManger script
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

public class GameManger : MonoBehaviour
{
    public GameObject PinPrefab; //The preset body of the pin 
    private Transform insPoint; //Instantiated position 
    public Pin currentPin; //Current pin 
    public  bool isOver; //Determine whether the current end is 
    public GameObject Circle;
     public Text scoreText; //Score Displayed text 
    public  int score = 0 ; //Equal score 
    public Camera mainCamera;
     public  int speed= 3 ; //The speed of the gradient 
    // Start is called before the first frame update 
    void  Start ()
     {
        insPoint = GameObject.Find("InsPoint").transform;
        InsPin();
        mainCamera = Camera.main; //Get the main camera
    }

    // Update is called once per frame 
    void  Update ()
     {
         if (isOver) return ;
         //Get mouse click 0 - left mouse button 1 - mouse right button 2 - mouse middle button 
        if (Input.GetMouseButtonDown( 0 ))
        {
            score++;
            scoreText.text = score.ToString();
            currentPin.StartFly();
            //Debug.Log("The left mouse button clicked");
            InsPin();
        }
    }
    //Instantiate the pin 
    void  InsPin ()  {
        currentPin = GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();
    }

    public void GameOver() {
        if (isOver) return;
        Circle.GetComponent<RotateSelf>().enabled = false;
        isOver = true;
        StartCoroutine(GameOverAnimation()); //Start the coroutine
    }
    // Coroutine 
    IEnumerator GameOverAnimation ()  {
         while ( true )
        { //Switch the color of the game over camera
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize,4,speed*Time.deltaTime);
            if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01)
            {
                break;
            }
            yield return 0;
        }
        // Delay call 
        yield return  new  WaitForSeconds ( 1 ) ;
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Mount the corresponding things according to this

  1. Then add the effect of the game over and
    modify the camera properties of the camera.
    This is what the modification looks like.

Because these are also my first learning, please bear with me if you have any questions~~~

You may also like...

Leave a Reply

Your email address will not be published.