コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】TransformのTranslate関数の拡張メソッド

ソースコード

using UnityEngine;

public static class TransformExtensions
{
    /// <summary>
    /// X 軸方向に移動します
    /// </summary>
    public static void TranslateX( this Transform self, float x )
    {
        self.Translate( x, 0, 0 );
    }
    
    /// <summary>
    /// Y 軸方向に移動します
    /// </summary>
    public static void TranslateY( this Transform self, float y )
    {
        self.Translate( 0, y, 0 );
    }
    
    /// <summary>
    /// Z 軸方向に移動します
    /// </summary>
    public static void TranslateZ( this Transform self, float z )
    {
        self.Translate( 0, 0, z );
    }
}

使い方

transform.TranslateX( 25 );
transform.TranslateY( 25 );
transform.TranslateZ( 25 );

関連記事