예제 #1
0
    public void StartGame(int length)
    {
        initLength = length;
        Debug.Log("You called StartGame! initLength = " + initLength);
        flagDie = false;
        ifDrop  = true;
        ifJump  = false;
        foodController.LetDestroyFood();
        foodController.LetSpawnFood();

        deadScreen.SetActive(false);
        timeLastPlay = Time.time;

        ifAlive = true;
        setBodyLength(1); // Destroy all the body before start newgame;
        transform.rotation = beginRotation;
        transform.position = beginPosition;
        body[0].position   = new Vector3(0, 0, 0);
        body[0].rotation   = Quaternion.identity;
        Headposition       = body[0].gameObject.transform.position;
        setBodyLength(initLength);
        // The first element in body is "Head", which can be set using unity-editor

        currentScore.gameObject.SetActive(true);
    }
예제 #2
0
    // I changed the "Layer" of the "Head" in "SNAKE" to SNAKE (using unity editor)
    // So that if a collison happens in the Head, it will pass that signal to the nearest Rigidbody, in this case it is the "SNAKE"'s Rigidbody
    // I also turned off the Sphere Collider in Sphere Prefab (using unity-editor)
    // So that if the tail touches the food it DO NOT destroy the food
    void OnCollisionEnter(Collision collision)
    {
        ifColided = true;
        // Create an instance, which links to SpawnFood.cs script
        // Because SpawnFood.cs script is attached to "Camera", we find "Camera" and get the component
        SpawnFood foodController = platform.GetComponent <SpawnFood>();

        Debug.Log("You collided with " + collision.gameObject);

        if (collision.gameObject.tag == "Food")
        {
            Destroy(collision.gameObject);
            AddBody();
            foodController.LetSpawnFood();
        }

        if (collision.gameObject.tag == "Obstacle")
        {
            ifAlive = false;
        }
    }