Transform In Unity With C#

In this tutorial, we going to look at the Transform properties in Unity also we’ll learn to Transform in the C# script.

The subject title will separate 2 parts. In the first part we going to learn all about Transform also we’ll use the Transform menu in Unity, in the second part we’ll learn how to use these properties in the C# script.

What is Transform?

Transform is a class that contains position, scale, and rotation. We can access all of these features through Transform.

If you are reading the Rigidbody article before this lesson, you can see that the velocity value is also used in the movements.

To be honest, the concept of “velocity” is more suitable for projects, but we will use position assuming it has not been learned in this lesson.

Transform Menu In Unity

Next to the Position menu, you will see 3 different values, these values show the location of your character in x, y, and z coordinates.

Rotation, its menu will give the direction of your character. It is divided into 3 as x, y and z values, just like position.

Finally, there is a scale menu that helps you to adjust the size of your object according to x, y and z variables.

Using Transform In C#

First, let’s learn how to use the position parameter on C # and then learn the other parameters on C #.

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

public class p1 : MonoBehaviour
{
 void Start()
 {
  transform.position = new Vector3(1, 2, 1);
 }

 void Update()
 {
        
 }
}

In order to change the position, you should assign the vector3 value you determined to the position parameter in the transform.

This is the same for other Transform parameters, you can also examine the uses of other parameters in the example below.

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

public class p2 : MonoBehaviour
{
 void Start()
 {
  transform.Rotate(0 , 3 , 0);
  transform.localScale = new Vector3(2, 2, 2);
 }

 void Update()
 {
        
 }
}

Rotate function is used to rotate the object, localScale is used to adjust the size of the object.

In games, Transform is used to determine the location, size and direction of the object when the game starts. Of course, it can be used in different areas.

Leave a Reply

Your email address will not be published. Required fields are marked *