์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ๋”ฐ๋ผ ํšŒ์ „ ์‹œํ‚ค๋˜,

๋งˆ์šฐ์Šค๊ฐ€ ์™ผ์ชฝ์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ์„๋• ์ƒํ•˜๋ฐ˜์ „๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ๋ ค๊ณ  ํ•˜๋Š”๋ฐ

๋ง‰ํ˜€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.



์–ด๋–ป๊ฒŒ ํ•ด๋„ ํ•ด๋‹น ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ ๋˜๋”๊ตฐ์š”...

์ด๋ ‡๊ฒŒ ๋˜์ง€ ์•Š๊ณ  ์ด์„ ์ •์ƒ์ ์œผ๋กœ ๋“ค๋„๋ก ํ•  ์ˆœ ์—†์„๊นŒ์š”?


์•„๋ž˜๋Š” ์ฝ”๋“œ ์ „๋ฌธ์ž…๋‹ˆ๋‹ค.


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Player : MonoBehaviour

{

ย  ย  public Vector2 inputVec;

ย  ย  public float speed;

ย  ย  public float playerScaleX;

ย  ย  public float playerScaleY;


ย  ย  Rigidbody2D rigid;


ย  ย  public GameObject weapon;


ย  ย  void Awake()

ย  ย  {

ย  ย  ย  ย  playerScaleX = transform.localScale.x;

ย  ย  ย  ย  playerScaleY = transform.localScale.y;


ย  ย  ย  ย  rigid = GetComponent<Rigidbody2D>();

ย  ย  }


ย  ย  void Update()

ย  ย  {

ย  ย  ย  ย  inputVec.x = Input.GetAxisRaw("Horizontal");

ย  ย  ย  ย  inputVec.y = Input.GetAxisRaw("Vertical");

ย  ย  }


ย  ย  void FixedUpdate()

ย  ย  {

ย  ย  ย  ย  Vector2 nextVec = inputVec.normalized * speed * Time.fixedDeltaTime;

ย  ย  ย  ย  rigid.MovePosition(rigid.position + nextVec);

ย  ย  }


ย  ย  void LateUpdate()

ย  ย  {

ย  ย  ย  ย  if (inputVec.x > 0)

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  transform.localScale = new Vector3(playerScaleX, playerScaleY, 1f);

ย  ย  ย  ย  }

ย  ย  ย  ย  else if (inputVec.x < 0)

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  transform.localScale = new Vector3(-playerScaleX, playerScaleY, 1f);

ย  ย  ย  ย  }


ย  ย  ย  ย  RotateWeaponTowardsMouse();

ย  ย  }


ย  ย  void RotateWeaponTowardsMouse()

ย  ย  {

ย  ย  ย  ย  Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

ย  ย  ย  ย  Vector3 directionToMouse = mousePosition - weapon.transform.position;

ย  ย  ย  ย  float angle = Mathf.Atan2(directionToMouse.y, directionToMouse.x) * Mathf.Rad2Deg;

ย  ย  ย  ย  weapon.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

ย  ย  }

}