using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class PlayerMove : MonoBehaviour

{

Vector2 mousePosition;

Vector2 objectPosition;

Camera camera;


public float Speed;


private void Start()

{

camera = GameObject.Find("Main Camera").GetComponent<Camera>();

}


private void Update()

{

objectPosition = this.gameObject.transform.position;


if (Input.GetMouseButton(0))

{

mousePosition = Input.mousePosition;

mousePosition = camera.ScreenToWorldPoint(mousePosition);


Debug.Log("ob: " + objectPosition);

Debug.Log("mo: " + mousePosition);

}

if (mousePosition != null)

{

transform.position = PosMove();

}

}


public Vector2 PosMove()

{

float incline = (mousePosition.y - objectPosition.y) / (mousePosition.x - objectPosition.x);

float angle = Mathf.Atan2(mousePosition.y - objectPosition.y, mousePosition.x - objectPosition.x);


float x_pos = objectPosition.x + Speed * Mathf.Cos(angle) * Time.deltaTime;

float y_pos = incline * (x_pos - objectPosition.x) + objectPosition.y;


Debug.Log("x: " + x_pos);

Debug.Log("y= " + y_pos);


Vector2 pos = new Vector2(x_pos, y_pos);

return pos;

}

}


--------------------------------------------------------------------------------------------------

์ด๋Ÿฐ ์‹์œผ๋กœ ๋งˆ์šฐ์Šค ์ขŒํด๋ฆญ ์‹œ์— ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ์˜ ์œ„์น˜๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ์ฝ”๋“œ๋ฅผ ์งฐ๋Š”๋ฐ ์ด๋™ ํ•œ ๋ฒˆ ์‹œํ‚ค๊ณ  ๋‚˜๋ฉด ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๊ณ„์† ๋ถ€๋“ค๋˜๋”๋ผ๊ณ ์—ฌ ์™œ ๊ทธ๋Ÿฐ๊ฐ€์—ฌ?





---------------------------------------------------------------------------------------------

์ˆ˜์ • ์™„๋ฃŒโ˜†


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class PlayerMove : MonoBehaviour

{

ย  ย  Vector2 mousePosition;

ย  ย  Vector2 objectPosition;

ย  ย  Camera camera;

ย  ย  public GameObject markerObj;


ย  ย  public float Speed;

ย  ย  float distance;


ย  ย  private void Start()

ย  ย  {

ย  ย  ย  ย  camera = GameObject.Find("Main Camera").GetComponent<Camera>();

ย  ย  }


ย  ย  private void Update()

ย  ย  {

ย  ย  ย  ย  objectPosition = this.gameObject.transform.position;


ย  ย  ย  ย  if (Input.GetMouseButton(1))

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  mousePosition = Input.mousePosition;

ย  ย  ย  ย  ย  ย  mousePosition = camera.ScreenToWorldPoint(mousePosition);


ย  ย  ย  ย  ย  ย  //Debug.Log("ob: " + objectPosition);

ย  ย  ย  ย  ย  ย  //Debug.Log("mo: " + mousePosition);

ย  ย  ย  ย  }


ย  ย  ย  ย  distance = Mathf.Sqrt(Mathf.Pow(mousePosition.x - objectPosition.x,2) + Mathf.Pow(mousePosition.y - objectPosition.y,2));

ย  ย  ย  ย ย 

ย  ย  }


ย  ย  private void FixedUpdate()

ย  ย  {

ย  ย  ย  ย  transform.position = PosMove();


ย  ย  ย  ย  if (distance < Speed * Time.deltaTime) transform.position = mousePosition;

ย  ย  }


ย  ย  public Vector2 PosMove()

ย  ย  {

ย  ย  ย  ย ย 

ย  ย  ย  ย  float incline = (mousePosition.y - objectPosition.y) / (mousePosition.x - objectPosition.x);

ย  ย  ย  ย  float angle = Mathf.Atan2(mousePosition.y - objectPosition.y, mousePosition.x - objectPosition.x);


ย  ย  ย  ย  float x_pos = objectPosition.x + Speed * Mathf.Cos(angle) * Time.deltaTime;

ย  ย  ย  ย  float y_pos = (mousePosition.x - objectPosition.x != 0) ? incline * (x_pos - objectPosition.x) + objectPosition.y : objectPosition.y;

ย  ย  ย  ย 


ย  ย  ย  ย  Vector2 pos = new Vector2(x_pos, y_pos);

ย  ย  ย  ย  return pos;

ย  ย  }

}