Game mechanics in geometry lesson or vectors in Unity 3D

Teaching materials for the school of programming. Part 16

Previous tutorials can be found here:
  1. Spaceship





  2. Dominoes





  3. Flappy bird





  4. Gravity room





  5. Platformer





  6. Trees (plugin SpeedTree)





  7. Modeling a house in SketchUp





  8. House in the woods





  9. Rain effect. Particles





  10. Billiards





  11. Liquid character





  12. Sticking and working with the Event System





  13. Unity 3D synthesizer





  14. Hovercraft





  15. Ragdolls on Unity 3D





In this article, we will turn our eyes to the past, and remember how the Step to Science children's programming school began. The initial idea of ​​the project was to be not just a circle of technical creativity, but to become an answer for children to the question "why go to school?"





Why do we need physics, algebra and geometry, if we do not plan to design spaceships, if we have a calculator in our phone for counting, we often pay with a card, so we don't even need to count change in our head.

As a child, I also conducted such reasoning, and my parents had no other ways to convey the truth to me, except for the phrase “I don’t want a word, I have to have a word” and a belt, which motivated me to sit down for lessons without unnecessary polemics.





With age, moving to the other side of the barricades, I realized that I wanted to explain, show, and prove to the children that it is really important to study at school! And the game project that we will analyze today is one of a cycle of classes for studying school subjects through games in Unity 3D.





The Unity cross-platform engine provides tremendous opportunities for the teacher: through the fascinating process of creating games, we study the laws of physics, geometry, do calculations, design the environment, use storytelling, scenario mechanics. And, of course, we program. There are countless options for integrating Unity into other educational and subject areas!





Execution order

2D «», (, , ). . LineRenderer .





!





.

, 2D .





, «» . , .





, Order in layer . , Circle collider Rigidbody.





Audio Source, .





, , .





using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {
    public AudioSource hitSound;
    public Rigidbody2D rig;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void FixedUpdate() {

    }

    private void OnCollisionEnter2D(Collision2D other) {
        if (other.relativeVelocity.magnitude > 1f) {
            hitSound.Play();
            hitSound.volume = Mathf.Clamp01(other.relativeVelocity.magnitude / 10);
            rig.velocity *= 0.8f;
        }
    }
}
      
      



Rigidbody, . Play, , . , .





, . , : LineRenderer, .





:





, LineRenderer', :





using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour {
   public Vector3 showVector;
    public LineRenderer lrenderer1;
    public LineRenderer lrenderer2;
    Transform myTransform;

    // Use this for initialization
    void Start () {
        //lrenderer1 = GetComponent<LineRenderer>();
        myTransform = transform;
    }

   // Update is called once per frame
    void Update () {
        showVector = new Vector3(showVector.x, showVector.y, 0f);

        lrenderer1.SetPosition(0, myTransform.position);
        lrenderer1.SetPosition(1, myTransform.position + showVector);
  
        if (showVector.magnitude >= 2f) { //  
            lrenderer2.SetPosition(0, myTransform.position + showVector - showVector.normalized);
        } else {
            lrenderer2.SetPosition(0, myTransform.position + showVector * 0.5f);
        }
        lrenderer2.SetPosition(1, myTransform.position + showVector);

        if (showVector.magnitude < 0.1f) {
            lrenderer1.enabled = lrenderer2.enabled = false;
        } else {
            lrenderer1.enabled = lrenderer2.enabled = true;
        }
    }
}
      
      



- .





, "" . :





using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VectorVelocity : MonoBehaviour {
    public Rigidbody2D rig;
    public Arrow arrow;

    // Use this for initialization
    void Start () {
 
    }

    // Update is called once per frame
    void Update () {
        if (rig.bodyType == RigidbodyType2D.Dynamic) {
            arrow.showVector =  rig.velocity / 5f;
        }
    }
}
      
      



, .





. 15 , . , - Trail Renderer .





, . Rigidbody Kinematic .





:





using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Spawner : MonoBehaviour {

    public Rigidbody2D ball;
    public TrailRenderer tr;

    Quaternion oldRotation;
    Vector3 oldPosition;

    public bool readyToShoot = true;

    // Use this for initialization
    void Start () {
        oldPosition = ball.transform.position;
        oldRotation = ball.transform.rotation;
    }

    // Update is called once per frame
    public void Respawn () {
        ball.transform.position = oldPosition;
        ball.transform.rotation = oldRotation;

        ball.velocity = Vector3.zero;
        ball.angularVelocity = 0;
        ball.bodyType = RigidbodyType2D.Kinematic;

        readyToShoot = true;
        tr.Clear();
    }

    public void Shoot(Vector3 speed) {
        if (!readyToShoot) {
            return;
        }

        ball.bodyType = RigidbodyType2D.Dynamic;
        ball.velocity = speed;
        readyToShoot = false;
    }
}
      
      



.





. , . UI -> Panel , TouchPanel.cs , .





( ):





, - , , .





, / , Toggle, - . .





!





PS Share the link to the article with colleagues, friends and curious students. It will be great if you try to do one of the lessons at your school or in the children's technical creativity circle, and write a few words of feedback on how the Unity 3D lesson went. Good luck!








All Articles