Scratch To Unity: Motion

Beginner Scratch Tutorial Unity

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

Entertainment Business Instructor Spotlight
Monday May 13, 2019

Winner of the 2019 Cynopsis PR Campaign at the Short Form Film Festival We’d like to send a heartfelt congratulations to one of our Entertainment Business instructors, Pam Newton, who wrote and published her first book A Candle for My Mother. The novel chronicles the journey of her mother’s desire to see the world while […]

The post Entertainment Business Instructor Spotlight appeared first on Dua xe moto.

Code Alternatives
Before you dive in, I wanted the opportunity to present a few alternatives for your animation needs. Unity has a lot of features you wont find in Scratch, so doing things the “Scratch way” inside of Unity may not actually be the best approach. Take a moment to learn the “Unity way” of doing things and see what works best for you.
In Unity, I may only program my motions when there is some dynamic nature involved. If I were creating a cut-scene or flying logo, or any other kind of animation that should always play out the same way, you can create those kinds of animations directly inside of the Unity interface without any code at all. Click here to watch a video tutorial by Unity.
If you want to do character animation, a lot of times animation will actually be created outside of Unity and imported in. The animation will then be controlled using an Animator component by a State Machine. Click here to learn more about that kind of setup.
Sometimes, even dynamic motion can be handled automatically. It really depends on how much control I require. In some cases, I am happy to allow Unity’s physics engine to determine where and how all of my object motion plays out. Click here to learn how Unity’s physics engine can improve and simplify your motion needs.
There are many more tutorials on animation and physics as well as a variety of topics all on Unity’s website. Click here to explore for yourself.
Code Blocks
Click on the picture of any of these Scratch code blocks to see how to write similar code statements in C#. For convenience I have created placeholder methods that show how to get a random position, and a mouse position in world space which some of the blocks may reference.

Hannah Lux Davis Breaks Records
Monday February 11, 2019

Featured image of Hannah Lux Davis from Create & Cultivate. 2006 LAFS Alumna Hannah Lux Davis directed music videos for GRAMMY®-winning artists Ariana Grande and Kacey Musgraves This year’s Grammys was a night of firsts. In case you missed the televised ceremony, there were many magical moments celebrating the best entertainers in the industry. The […]

The post Hannah Lux Davis Breaks Records appeared first on Dua xe moto.

transform.Translate(10, 0, 0);

transform.Rotate(0, 0, -15);

transform.Rotate(0, 0, 15);

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.

transform.localPosition = RandomPosition();

See RandomPosition.

transform.localPosition = new Vector3(0, 0, 0);

void Start()
{
var pos = RandomPosition();
var glide = Glide(1, pos);
StartCoroutine(glide);
}

See Glide.

void Start()
{
var pos = new Vector3(0, 0, 0);
var glide = Glide(1, pos);
StartCoroutine(glide);
}

transform.localEulerAngles = new Vector3(0, 0, 90);

transform.LookAt(MousePosition());

var pos = transform.localPosition;
pos.x += 10;
transform.localPosition = pos;

var pos = transform.localPosition;
pos.x = 0;
transform.localPosition = pos;

var pos = transform.localPosition;
pos.y += 10;
transform.localPosition = pos;

[csharp]
var pos = transform.localPosition;
pos.y = 0;
transform.localPosition = pos;

void OnCollisionEnter(Collision collision)
{
transform.forward = Vector3.Reflect(transform.forward, collision.contacts[0].normal);
}

I am not aware of anything that is a good match for this code block. The closest match I can think of is if you are allowing phyiscs to animate your game objects and add constraints so that they can only rotate on a certain axis. The code below would only rotate around the vertical axis, kind of like a globe.

var rigidBody = GetComponent<Rigidbody>();
rigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;

var xPos = transform.localPosition.x;

var yPos = transform.localPosition.y;

var direction = transform.localEulerAngles.z;

This shows a sample of obtaining a random position. Unlike the stage in Scratch, the world in Unity is unbounded. I have imposed my own range of values to pick from.

Vector3 RandomPosition()
{
var min = Vector3.zero;
var max = new Vector3(10, 10, 10);
return RandomPosition(min, max);
}

Vector3 RandomPosition(Vector3 min, Vector3 max)
{
var x = Random.Range(min.x, max.x);
var y = Random.Range(min.y, max.y);
var z = Random.Range(min.z, max.z);
return new Vector3(x, y, z);
}

This method shows a sample of obtaining a world position given the mouse position. Since Unity works in 3D space, we ray-cast from the screen coordinates of the mouse, to the hit location in world space. Note that you have to have an object with a collider in view of the camera for this to work.

Vector3 MousePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
return hit.point;
}
return Vector3.zero;
}

This method shows a sample of animating an object of the specified number of seconds, to the specified local position.

IEnumerator Glide(float seconds, Vector3 position)
{
var start = transform.position;
var time = 0f;
while (time < seconds)
{
yield return null;
time += Time.deltaTime;
var t = Mathf.Clamp01(time / seconds);
transform.position = Vector3.Lerp(start, position, t);
}
}

Links
Here are a few links that are relevant to this material and can help you dive a little deeper if you need more help.

Random
Mathf
String
Scratch Wiki for Operators Block

Summary
This should cover all of the code blocks in Scratch’s “Motion” 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!