대외 활동/유니티 스터디
[unity] class5_3D게임
ballbig
2021. 8. 6. 00:03
728x90
추가로 구현해본 것
1. 현재 점수를 particle로 표시해 주었다.
2. 총 점수 합계를 표시해 주었다.
3. 밤송이가 과녁을 맞추지 못한 경우 Destroy해주엇다.
[전체코드]
1. BamController
: 밤송이를 날리고, 밤송이가 과녁에 맞는지 여부를 검사한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class hw6Bamcontroller : MonoBehaviour
{
public GameObject BamGenerator;
public GameObject assist;
float x1, x2, y1, y2;
public void Shoot(Vector3 dir) //인자로 3차원 벡터가 입력되고
{
GetComponent<Rigidbody>().AddForce(dir); // 들어온 입력벡터 만큼 오브젝트에 힘이 가해진다.
}
// Start is called before the first frame update
void Start()
{
this.BamGenerator = GameObject.Find("BamGenerator");
this.assist = GameObject.Find("assist");
}
// Update is called once per frame
void Update()
{
}
public void OnCollisionEnter(Collision collision) //다른 물체와 충돌하는 순간
{
GetComponent<Rigidbody>().isKinematic = true; //중력의 영향을 무시
GetComponent<ParticleSystem>().Play(); // particle효과를 실행시킨다.
Vector2 p1 = this.transform.position;
Vector2 p2 = this.assist.transform.position;
float d = (p1 - p2).magnitude;
int n = Mathf.CeilToInt(10 - d * 5);
if (collision.gameObject.tag == "target") //target과 충돌하는 경우 점수 상승
{
BamGenerator.GetComponent<hw6BamGenerator>().scorePlus(n);
}
else if((collision.gameObject.tag == "terrain")) //바닥과 충돌하는 경우 삭제
{
Destroy(gameObject);
}
}
}
2. BamGenerator
: 밤송이를 생성하고, 과녁 위치에 따른 점수를 화면에 출력한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class hw6BamGenerator : MonoBehaviour
{
public GameObject score;
//public GameObject current;
public GameObject bamPrefab;
public GameObject number;
public int num = 0;
public Material[] mat = new Material[3];
void Start()
{
this.score = GameObject.Find("score");
//this.current = GameObject.Find("current");
this.number = GameObject.Find("number");
for (int i = 0; i<10; i++)
{
mat[i] = Instantiate(mat[i]) as Material;
}
}
void Update()
{
if (Input.GetMouseButtonDown(0)) //마우스버튼을 누르는 경우
{
GameObject bam = Instantiate(bamPrefab) as GameObject; //밤 오브젝트를 생성하고
//마우스가 탭한 위치를 스크린 좌표계로 변환시켜준다.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //인자로 들어온 좌표로 향하는 광선(Ray) 클래스를 반환한다.
Vector3 worldDir = ray.direction; //
bam.GetComponent<hw6Bamcontroller>().Shoot(worldDir.normalized * 2000); //BamController스크립트에서 작성한 Shoot함수를 호출시킨다.
}
}
public void scorePlus(int n)
{
num += n;
if(n != 0)
{
this.number.GetComponent<ParticleSystemRenderer>().material = mat[n-1]; //점수 particle 효과
this.number.GetComponent<ParticleSystem>().Play(); //점수 particle 효과
}
this.score.GetComponent<Text>().text = "Total : " + num;
//this.current.GetComponent<Text>().text = n.ToString();
}
}
자세한 제작과정은 다음 포스팅으로!
https://bigballdiary.tistory.com/30
[unity] class5_3D게임_제작과정1(지형설정, paint)
1. Lighting설정하기 Directional Light만으로는 어둡기 때문에 Lighting을 추가해 주어야 한다. (*이때, Auto Generate 체크는 해제한다.) 2. 지형배치하기_Terrain 1) 지형 생성하기 3D object -> Terrain을..
bigballdiary.tistory.com