Unity Methods

JunDev·2025년 3월 11일

Unity

목록 보기
4/8

Transform

Transform Method

  • transform is a property that refers to the Transform component of a GameObject.
  • It holds data for position, rotation, and scale.
  • You directly assign values to transform.position, transform.rotation, or transform.localScale.

transform.position

void Update()
{
    transform.position = new Vector3(1, 2, 3);  // Moves the object to (1,2,3)
}

transform.Translate()

void Update()
{
    transform.Translate(Vector3.forward * Time.deltaTime * 5); 
}

InvokeRepeating()

InvokeRepeating() is a built-in Unity function that repeatedly calls a method at fixed time intervals.

InvokeRepeating("MethodName", initialDelay, repeatRate);
// Example
using UnityEngine;

public class AutoShooter : MonoBehaviour
{
    public GameObject bulletPrefab;

    void Start()
    {
        InvokeRepeating("Shoot", 1f, 0.5f); // Start shooting after 1s, then every 0.5s
    }

    void Shoot()
    {
        Instantiate(bulletPrefab, transform.position, Quaternion.identity);
    }
}

Instantiate()

Instantiate() is a Unity method used to create copies (or clones) of GameObjects or prefabs at runtime.

Instantiate(original, position, rotation);
// Example
public GameObject bulletPrefab; // Bullet prefab to instantiate

void Shoot()
{
    Instantiate(bulletPrefab, transform.position, Quaternion.identity); // Spawn bullet at Launcher's position
}

Destroy()

Destroy() is a Unity method used to remove or delete a GameObject, Component, or Asset from the scene, usually during runtime.

Destroy(object);
// Example
public GameObject enemy;

void DestroyEnemy()
{
    Destroy(enemy); // Destroys the 'enemy' GameObject
}
profile
Jun's Dev Journey

0개의 댓글