Unity Mouse Effect

GonoBae
7 min readDec 10, 2021

--

지난 글에서 만든 것에 마우스 이펙트만 넣어보자.

우선 마우스 이펙트는 마우스 위치를 받아와서 만들어야 한다.

그리고 캐릭터도 마우스 위치를 받아온다.

따라서 같은 기능을 여기저기서 쓰기 때문에 코드를 살짝 수정한다.

EffectManager 클래스에 Singleton 을 적용하여 코드를 작성하자.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EffectManager : MonoBehaviour
{
#region Singleton
private static EffectManager _instance = null;
public static EffectManager instance
{
get
{
if(!_instance) FindObjectOfType<EffectManager>();
return _instance;
}
}
#endregion
public Camera cam;
private void Awake()
{
if(_instance != null && _instance != this) Destroy(gameObject);
else _instance = this;
}
public Vector3 GetMousePosition()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
return hit.point;
}
return Vector3.zero;
}
}

다음과 같이 EffectManager 에서 GetMousePosition 함수를 작성하여

공유하도록 해보자.

이제 Player Script 로 가서

using UnityEngine;
using UnityEngine.AI;
public class Player : MonoBehaviour
{
private NavMeshAgent agent;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if(Input.GetMouseButtonDown(1)) agent.SetDestination(
EffectManager.instance.GetMousePosition());
}
}

다음과 같이 수정하면 이전과 동일하게 작동한다.

이제 MouseEffect 를 만들어보자.

3D Object → Plane 을 만들어주자.

Plane 에 Image 를 그냥 Drag & Drop 해주자.

이제 Image 만 보이게 Shader 를 수정해보자.

성공!

스케일을 조정해주고 필요없는 Collider 를 지워주자.

이제 Plane 의 이름을 MouseEffect 로 수정하고 계속해보자.

이제 MouseEffect 의 Script 와 이들을 관리해줄 List

그리고 Spawn 을 해줄 함수를 만들어보자.

우선 MouseEffect 라는 Script 를 만들어주자. (List 를 만들 때 필요)

EffectManager Script 로 가서 다음을 추가해주자.

public List<MouseEffect> lstMouseEffect;
public void Spawn_MouseEffect(Vector3 pos)
{
List<MouseEffect> lstTmp = lstMouseEffect;
foreach(var tmp in lstTmp)
{
if(tmp.gameObject.activeSelf == false)
{
tmp.transform.position = pos + new Vector3(0, 0.05f, 0);
tmp.gameObject.SetActive(true);
return;
}
}
}

Spawn 함수는 반복문을 돌며 List 에서 false 인 항목을 확인하여

해당 객체의 위치를 받아온 위치에서 Y 축으로 0.05 만큼 증가시켜

Spawn 시킨다.

Y 축으로 올리는 이유는 맵의 두께로 인해 바닥에 파묻히지 않게 해주기 위함!

이제 Player 에서 오른쪽 키를 누르면 해당 함수를 호출해주자.

private void Update()
{
if(Input.GetMouseButtonDown(1))
{
Vector3 mousePos = EffectManager.instance.GetMousePosition();
EffectManager.instance.Spawn_MouseEffect(mousePos);
agent.SetDestination(mousePos);
}
}

GetMousePosition 함수를 이펙트와 이동에서 모두 필요하기 때문에

mousePos 에 저장하고 전달해주는 것이 편리하다.

우선 Spawn 은 되는데 이펙트가 이펙트 같지 않다.

그리고 사라지지도 않는다.

아까 만들어준 MouseEffect Script 에 약간의 코드를 넣어주자.

using System.Collections;
using UnityEngine;
public class MouseEffect : MonoBehaviour
{
private void OnEnable()
{
StartCoroutine("ScaleChange");
}
private IEnumerator ScaleChange()
{
int count = 3;
while(count > 0)
{
transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
yield return new WaitForSeconds(0.1f);
transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
yield return new WaitForSeconds(0.1f);
count--;
}
this.gameObject.SetActive(false);
}
}

OnEnable 함수는 객체가 true 될 때마다 동작한다.

Awake 나 Start 는 최초 한 번만 실행되기에 적절하지 않다.

다음 시간에는 효과를 좀 더 자연스럽게 만들어보도록 하자.

오늘은 여기까지 :)

--

--