본문 바로가기
개발/C#

유니티 C# 자석 효과 Magnet 간단 구현

by SPNK 2022. 7. 6.
반응형
  • 코드 작성 (적용하고 싶은 오브젝트에 적용)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    public Transform target;

    public float moveSpeed = 1.0f;

    private void Update()
    {
        Vector2 relativePos = target.transform.position - transform.position;
        float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
        transform.localRotation = Quaternion.Euler(0, 0, angle - 90);
        transform.Translate(transform.up * moveSpeed * Time.deltaTime, Space.World);
    }
}
반응형

댓글