유니티 기초
밤송이 던지기
송현호
2023. 8. 4. 12:58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BamSongiGenerator : MonoBehaviour
{
public GameObject bamsongiPrefab;
// Start is called before the first frame update
public void Awake()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 1000f,Color.red, 1f);
Vector3 force = ray.direction.normalized * 2000f;
this.CreateBamsongi(force);
}
}
private void CreateBamsongi(Vector3 force)
{
GameObject go = Instantiate(this.bamsongiPrefab);
BamSongiController controller = go.GetComponent<BamSongiController>();
controller.Shoot(force);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BamSongiController : MonoBehaviour
{
Rigidbody rBody;
public int forwardForce = 1000;
private ParticleSystem effect;
// Start is called before the first frame update
private void Awake()
{
this.rBody = this.GetComponent<Rigidbody>();
this.effect = this.GetComponent<ParticleSystem>();
Debug.Log("Awake");
}
void Start()
{
}
public void Shoot(Vector3 force)
{
//앞 : (0,0,1)
this.rBody.AddForce(force);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "target")
{
Debug.LogFormat("과녁에 충돌");
this.rBody.isKinematic = true;
this.effect.Play();
}
}
}