Scratch To Unity: Sensing

Beginner Scratch Tutorial Unity

This post provides a handy reference guide to go from Scratch’s “Sensing” category of code blocks to C# with Unity.

See Our Alumni Credits at the 2019 Grammys
Tuesday February 05, 2019

What do Stranger Things, Ariana Grande and the Grammys have in common? The Los Angeles Film School alumni! It’s been quite a year for our alumni. Hannah Lux Davis (Film 2006) broke YouTube’s record for most views in 24 hours with Ariana Grande’s Thank U, Next video. Andres Borda (Recording Arts 2010) won a Latin […]

The post See Our Alumni Credits at the 2019 Grammys appeared first on Dua xe moto.

Code Blocks
Click on the picture of any of these Scratch code blocks to see how to write similar code statements in C#.

20th Anniversary Alumni Spotlight: Darryl Crutchfield
Tuesday February 19, 2019

Darryl Crutchfield in Chaplin A recent graduate shares insight on life after graduating with an associate degree in recording arts. Meet Darryl. Aka The one-and-only Dorell. He’s a big-hearted entertainment business professional who spends his time serving the veterans’ community through a project called Saving All Kids. When he’s not working on his side projects, […]

The post 20th Anniversary Alumni Spotlight: Darryl Crutchfield appeared first on Dua xe moto.

void OnMouseOver()
{
// Code to run when mouse is touching
}

The Physics engine in Unity is not based on pixel color. It is based on Collider shapes. You can mask collisions to control what collides with what, or wait for a collision and then check properties of the objects in the relevant callbacks:

void OnCollisionEnter(Collision collision)
{
// Check "color" of objects
}

Film & Game Releases: Week of October 12
Tuesday October 09, 2018

CHECK OUT OUR LIST OF MOVIE AND GAME RELEASES FOR THE WEEK OF October 12, 2018! From First Man to The Occupation, here is a schedule of the films and games coming out soon! Film Release Schedule Bad Times at the El Royale First Man Goosebumps: Haunted Halloween Game Release Schedule Disgaea 1 Complete (PS4, […]

The post Film & Game Releases: Week of October 12 appeared first on Dua xe moto.

Assuming an object is in screen space coordinates, like a UI Element, then you could use something like this:

var distance = Vector3.Distance(transform.position, Input.mousePosition);

See the “Motion” reference guide for examples of ray casting from the mouse position into the 3D world.

There is no built-in equivalent to this code block, but you can make your own. From the Hierarchy pane click the “Create” button and choose “UI” -> “Input Field”. You can add a handler method to the “On End Edit” event either in the Inspector or in code.
To work with an Input Field in code you must add the using declaration:

using UnityEngine.UI;

Here is an example of attaching a handler to the “On End Edit” event:

private void Start()
{
var input = GetComponent<InputField>();
input.onEndEdit.AddListener(OnEndEdit);
}

void OnEndEdit(string result)
{
Debug.Log(result);
}

if (Input.GetKey(KeyCode.Space))
{
// Code to run when key is pressed
}

if (Input.GetMouseButton(0))
{
// Code to run when mouse button is pressed
Debug.Log(Time.timeSinceLevelLoad.ToString());
}

var mouseX = Input.mousePosition.x;

var mouseY = Input.mousePosition.y;

Unity does not provide a built-in solution for dragging objects with the mouse. You can create your own by detecting mouse events and moving objects based on “Input.mousePosition”. If you create a component to handle dragging, then you can disable dragging by disabling the component.

In Unity, there is no exposed “loudness” variable. You have “volume” on “AudioSource” components, but that is how loud to play the clip as a whole, not how loud is the sound that is actually being heard. In other words, imagine you have turned the volume knob of a speaker all the way up. When sound plays, it will be loud, but there may be moments between sounds where there is nothing to hear.
In order to analyze the loudness of what is actually playing, you can sample your audio data. Here is a post on the Unity Answers forum that discusses this topic in detail, complete with code.

Unity does not have a built-in timer. You can track the passing of time using the “Time” class. See here. A simple timer might look something like this.

float timer = 10f;

void Update()
{
if (timer > 0)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
Debug.Log("Time's up!");
}
}
}

Backdrops are not special entities in Unity. You could create one as a Sprite just like you would create any other sprite. If you had a collection of backgrounds to use, then you could track the index of the background when you assign it on your own.

To work with date data types, you will need to add a using declaration:

using System;

You can then obtain the current year like this:

var currentDate = DateTime.Now;
var year = currentDate.Year;

To work with date data types, you will need to add a using declaration:

using System;

You can then obtain the number of days since 2000 like this:

var oldDate = new DateTime(2000, 1, 1);
var currentDate = DateTime.Now;
var totalDays = (currentDate – oldDate).TotalDays;

There is no equivalent to a username built-in to Unity. This is an advanced task that requires a server to implement. You might wish to pursue 3rd party libraries for this purpose such as Firebase.
Summary
This should cover all of the code blocks in Scratch’s “Sensing” category. If you have any questions about this reference guide feel free to ask below.
If you find value in my blog, you can support its continued development by becoming my patron. Visit my Patreon page here. Thanks!