Skip to content

Instantly share code, notes, and snippets.

@farukcan
Created January 14, 2022 20:13
Show Gist options
  • Save farukcan/cc64ff687f513b49ece998e381a3c488 to your computer and use it in GitHub Desktop.
Save farukcan/cc64ff687f513b49ece998e381a3c488 to your computer and use it in GitHub Desktop.
Axis Extension For Unity Engine To Handle Axises Quickly
// Author: github.com/farukcan
// Usage:
// Get vector - AxisExtension.GetVector(Axis.LEFT)
// Get direction vector relative to the object - AxisExtension.GetVector(transform,Axis.LEFT)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class AxisExtension
{
public enum Axis { UP, DOWN, FORWARD, BACK, LEFT, RIGHT, ZERO };
public static Vector3 GetVector(Axis axis)
{
if (axis == Axis.BACK)
{
return Vector3.back;
}
if (axis == Axis.FORWARD)
{
return Vector3.forward;
}
if (axis == Axis.LEFT)
{
return Vector3.left;
}
if (axis == Axis.RIGHT)
{
return Vector3.right;
}
if (axis == Axis.UP)
{
return Vector3.up;
}
if (axis == Axis.DOWN)
{
return Vector3.down;
}
return Vector3.zero;
}
public static Vector3 GetVector(Transform transform,Axis axis)
{
if (axis == Axis.BACK)
{
return -transform.forward;
}
if (axis == Axis.FORWARD)
{
return transform.forward;
}
if (axis == Axis.LEFT)
{
return -transform.right;
}
if (axis == Axis.RIGHT)
{
return transform.right;
}
if (axis == Axis.UP)
{
return transform.up;
}
if (axis == Axis.DOWN)
{
return -transform.up;
}
return Vector3.zero;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment