유니티 기초
유니티 밤송이 던지기
EasyCastleT
2023. 8. 4. 13:02
파티클 시스템을 사용했음
BamsongiController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BamsongiController : MonoBehaviour
{
private Rigidbody rBody;
[SerializeField]
private float forwardForce = 2000;
private ParticleSystem effect;
// Start is called before the first frame update
private void Awake()
{
this.rBody = GetComponent<Rigidbody>();
this.effect = GetComponent<ParticleSystem>();
}
void Start()
{
//this.Shoot();
}
// Update is called once per frame
public void Shoot(Vector3 force)
{
//앞으로 힘을 줘서 이동시킨다.
//앞 :(0,0,1) (World position) 항상 변하지 않음
//밤송이가 바라보는 앞쪽 (Local position) 변할 수 있음
this.rBody.AddForce(force); //포물선 그리기
}
private void OnCollisionEnter(Collision collision)
{
//충돌한 대상의 게임오브젝트 이름
if(collision.gameObject.tag == "Target")
{
Debug.Log("과녁에 충돌");
this.rBody.isKinematic = true;
this.effect.Play();
}
}
}
BamsongiGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BamsongiGenerator : MonoBehaviour
{
public GameObject bamsongiPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
//화면을 터치 하면 월드 공간에 Ray를 생성함
//픽셀 좌표계를 월드 좌표계로 반환하여 Ray생성
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//생성된 레이를 에디터에서 출력
Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
//ray.direction.normalized(단위 벡터로 변경)
//길이가 1인 벡터 : 방향
Vector3 force = ray.direction.normalized * 2000f; //방향 * 힘:2000f
this.CreateBamsongi(force);
}
}
private void CreateBamsongi(Vector3 force)
{
GameObject go = Instantiate(bamsongiPrefab);
BamsongiController controller = go.GetComponent<BamsongiController>();
//Debug.LogError(go.transform.position);
controller.Shoot(force);
}
}
결과
타겟을 클릭할 때는 제대로 기능하지만, 타겟에서 멀어질 수록 점점 정확도가 떨어지는 문제가 있다.
이 문제에 대해서 나중에 고찰을 해보기로 하자