Transform Extensions - BNS-MarkUlrich/MarksMagicToolbox GitHub Wiki

Overview

TransformExtensions is a static class containing various extension methods that manipulate the transform properties of GameObjects in Unity.

Installation

To use TransformExtensions in your Unity project, follow these steps:

  1. Copy the TransformExtensions.cs file into your Unity project's scripts directory.
  2. Ensure the System.Collections.Generic and UnityEngine namespaces are included.

Usage

Methods

Method Description
LookAt(this GameObject target, Vector2 direction) Rotates the target GameObject to face the specified direction.
SetPositionX(this Transform target, float xPosition) Sets the X position of the target Transform to the specified value.
SetPositionY(this Transform target, float yPosition) Sets the Y position of the target Transform to the specified value.
SetPositionZ(this Transform target, float zPosition) Sets the Z position of the target Transform to the specified value.
Hide(this GameObject target, bool isHidden = true) Hides or reveals the target GameObject by adjusting its scale (Default: hides by setting scale to zero).

Parameters

  • target: The GameObject or Transform the method is applied to.
  • direction: The direction the GameObject should face (used in the LookAt method).
  • xPosition, yPosition, zPosition: The values to set the X, Y, or Z positions of the target Transform.
  • isHidden: Boolean flag indicating whether to hide (true) or reveal (false) the GameObject.

Example

using UnityEngine;

public class TransformExtensionsExample : MonoBehaviour
{
    void Start()
    {
        GameObject player = new GameObject("Player");

        // Set Player position
        player.transform.SetPositionX(5f);
        player.transform.SetPositionY(2f);
        player.transform.SetPositionZ(0f);

        // Rotate player to face a direction
        Vector2 direction = new Vector2(1f, 1f);
        player.LookAt(direction);

        // Hide the player
        player.Hide();
    }
}

Repository

TransformExtensions.cs

Discover extension methods that manipulate the transform properties of GameObjects.