ย public class FourWayController : MonoBehaviour

ย  ย  {

ย  ย  ย  ย  private Vector3[] directionalVectors = { Vector3.forward, Vector3.back, Vector3.right, Vector3.left };


ย  ย  ย  ย  private Transform _mainCameraTransform;


ย  ย  ย  ย  private void Awake()

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  _mainCameraTransform = Camera.main.transform;

ย  ย  ย  ย  }


ย  ย  ย  ย  private void Update()

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  var movementVector = new Vector3(CnInputManager.GetAxis("Horizontal"), 0f, 0f);

ย  ย  ย  ย  ย  ย  if (movementVector.sqrMagnitude < 0.00001f) return;


ย  ย  ย  ย  ย  ย  // Clamping

ย  ย  ย  ย  ย  ย  Vector3 closestDirectionVector = directionalVectors[0];

ย  ย  ย  ย  ย  ย  float closestDot = Vector3.Dot(movementVector, closestDirectionVector);

ย  ย  ย  ย  ย  ย  for (int i = 1; i < directionalVectors.Length; i++)

ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  float dot = Vector3.Dot(movementVector, directionalVectors[i]);

ย  ย  ย  ย  ย  ย  ย  ย  if (dot < closestDot)

ย  ย  ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  closestDirectionVector = directionalVectors[i];

ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  closestDot = dot;

ย  ย  ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  ย  ย  }


ย  ย  ย  ย  ย  ย  // closestDirectionVector is what we need

ย  ย  ย  ย  ย  ย  var transformedDirection = _mainCameraTransform.InverseTransformDirection(closestDirectionVector);

ย  ย  ย  ย  ย  ย  transformedDirection.y = 0f;

ย  ย  ย  ย  ย  ย  transformedDirection.Normalize();


ย  ย  ย  ย  ย  ย  transform.position += transformedDirection * Time.deltaTime;

ย  ย  ย  ย  }

ย  ย  }

}

์ด์ œ ์ž˜๋˜์„œ ๋‚ด๊บผ์— ์ด์‹ ์‹œํ‚ค๋ ค๊ณ .

์ด๋ ‡๊ฒŒํ•ด์„œ ์•„๋ฌด๊ฒƒ๋„ ์•ˆ๊ฑด๋“œ๋ฆฌ๊ณ  ์œ„์— 0์œผ๋กœ๋งŒ ๋ฐ”๊ฟ”์ค€๊ฑฐ๋ฐ–์—์—†๋Š”๋ฐ.. ๋‚ด ์ƒ๊ฐ์— 3d์˜€๋˜๊ฒŒ 2d์— ์ ์šฉ๋˜์„œ๊ทธ๋Ÿฐ๊ฐ€? ย ์ขŒ ์šฐ๊ฐ€ ๋ฐ”๋€Œ๋˜๋ฐ.. ์™ผ์ชฝ๋ˆ„๋ฅด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๊ณ .. ์˜ค๋ฅธ์ชฝ๋ˆ„๋ฅด๋ฉด ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋„ค.. ใ„ทใ„ท ์™œ๊ทธ๋Ÿฐ๊ฑธ๊นŒ..


์•„! ๊ทธ๋ฆฌ๊ณ  ์†๋„๊ฐ€ ๋„ˆ๋ฌด ๋А๋ฆฐ๋ฐ.. ์†๋„ ์–ด๋–ป๊ฒŒ ...