Contact us to obtain full access!

Including...

Self-Grading Assessments

Project-Based Learning

Attendance Tracking

Student Data & Reporting

Professional Development

Unity Certification Institute

4.3.2 Button Interactions

In this section, students will be adding interactive buttons to the Roll-a-Ball game that allow the player to activate different parts of the level.

Contact us to get the full experience and try out our LMS with Self Grading Quizzes, Gradebook, Attendance Tracking, Student Reporting, Discussion Board Features and more!

Contact Us

Slides.pdf

Slides.pptx

Learning Objectives

Upon completing this section, students will be able to:

  • use the Unity Spring Joint component to create spring-like physics interactions.
  • use a combination of colliders in a single prefab to contain a specific interaction.
  • use the unity event system to broadcast events that trigger interactions in other gameobjects.
  • understand and use smooth interpolation to control the motion of kinematic rigidbodies.

Lesson Overview

Spring Joints

  • To get the button pad to move up and down as a button normally would, we can use a Spring Joint component.
  • A Spring Joint is like an invisible spring connecting one body to another.
  • The Anchor is where the spring’s force is applied.
  • The Spring property controls the magnitude of the spring force when it is stretched or compressed away from its resting point.
  • The Damper property is like drag.

Button Movement

  • We can use two Box Colliders to approximate the shape of the button pad.
  • A Spring Joint is added to the button pad, and the button holder is set as the Connected Body.
  • To prevent unwanted movement of the button pad, we can constrain its movement to be only vertical using the constraint properties in its Rigidbody component.

Button Functionality

  • A trigger box collider on the Button prefab is placed below the button pad so that when the pad is pressed down, their colliders touch.
  • The Button script detects when the button is pressed down and calls a script on other GameObjects using SendMessage.
  • To prevent the code in OnTriggerEnter from executing twice, we create a bool debounce and set it to true in OnTriggerEnter and return when debounce is true.

Elevator Movement

  • The wood palette is animated to move up and down like an elevator.
  • In the BtnToggleAnimation script, we create an OnButtonDown method, which is called in the Button script when the player presses the button.
  • OnButtonDown toggles the animation on and off by calling SetTargetSpeed, which sets the target speed to 1 or 0.
  • Inside Update, the animator.speed is smoothly transitioned using linear interpolation (Mathf.Lerp).

Unity Scripting API

Mathf.Lerp: Calls the method with the specific name on every MonoBehaviour in the game object it is attached to.

GameObject.SendMessage: Linearly interpolates between two values over a period of time. The parameter t is clamped to the range [0, 1].

Unity Manual

Spring Joint: The spring acts like a piece of elastic that tries to pull the two anchor points together to the exact same position. The strength of the pull is proportional to the current distance between the points with the force per unit of distance set by the Spring property.

Roll-a-Ball

Students create their first fully-featured game using an array of assets including 3D models, textures, audio files, and C# scripts.

Play in Browser