via-10: 생성,파괴
Tip
호출된 함수 드래그해서 F12누르면, 함수정의부분으로 이동함 함수정의부분 위에 참조1개 버튼을 누르면 함수호출부분으로 이동함 방금까지 작업했전 곳으로 이동: Ctrl+ - 입력정보, 부딪힌 정보를 받아 올때는 get 명령할때는 set activeself: 켜져있는지 꺼져있는지 확인 비활성화된 오브젝트 안의 스크립트는 활성화 되지 않음
2단점프 가능하게 하기
int _jumpNum;
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 0)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 2)
return;
}
껐다 켜기
- 어떤 오브젝트가 있는데
- 마우스 클릭할때마다
- 만약에 현재 오브젝트가 꺼져있다면 , 오브젝트를 켜주시구요 그게아니라 오브젝트가 꺼져있으면 켜주세요
1. 유니티 클라이언트에서 작업
2. 사용하는 오브젝트를 변수로 만듦
public GameoObject obj;
3. 스크립트 작성
3-1 마우스를 클릭할때 마다 껐다 켰다하기(SetActive,activeSelf)
if (Input.GetMouseButtonDown(0))
{
if(obj.gameObject.activeSelf == true)
{
obj.gameObject.SetActive(false);
}
else
{
obj.gameObject.SetActive(true);
}
}
생성, 파괴
prefab
- custom한 상태의 gameobject를 실제 프로젝트 파일로 빠져나옴
- Hierarchy에 gameobject를 실수로 삭제해도, Assets에 있으니 다시 넣을수 있음
생성
1. 마우스클릭할때마다
2. 빨간색 공이 생성되게 해주세요
- 프로젝트폴더(Assts)에있는 파일을 스크립트의 obj에 넣어야함
2-1 오브젝트만 소환
#### 2-2 오브젝트를 소환하고 어떠한 트랜스폼을 주어주는 경우
```Instantiate(obj.gameObject, this.gameObject.transform);```
#### 2-3 소환하면서 위치와 회전을 주는경우
```Instantiate(obj.gameObject, new Vector3(5,5,5),Quaternion.Euler(0,0,0))```
#### 2-4 소환한 공의 색깔을 파란색으로 만들어라
```GameObject = insObj.GetComponent<MeshRenderer>().materials[0].color = Color.Blue;```
- 버그가 날 일이 없음
- 지역변수 사용
- 3초 또는 5초 후에 뭔가를 할려고 할때 안됨
#### 2-5 소환한 공의 useGravity를 끈 상태로 만들어라
```GameObject = insObj.GetComponent<Rigidbody>().useGravity = false```
#### 2-6 마우스 포인트에 생성하게 해주세요
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit rHit; //int Mask = 1 « LayerMask.NameToLayer(“Floor”); if(Physics.Raycast(r,out rHit, 100)) { }
```c#
void RayPoint()
{
if (Input.GetMouseButtonDown(1))
{
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rHit;
if (Physics.Raycast(r, out rHit, 100))
{
Instantiate(obj.gameObject, rHit.point, Quaternion.Euler(0, 0, 0));
print(rHit.collider.gameObject.name); //부딪힌 오브젝트의 이름 나오기
}
}
}
2-7 ray 보여주기
Debug.DrawRay(new Vector3(0,1,0), new Vector3(0,5,0),Color.blue);
최종
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class Player : MonoBehaviour
{
public int _moveSpeed;
public int _addforce = 100;
public Transform[] cameraTr;
int cameraNum;
int _jumpNum;
public GameObject obj;
public GameObject pefabs;
void Start()
{
cameraNum = 0;
_jumpNum = 0;
}
void Update()
{
Move();
Jump();
See();
RayPoint();
//if (Input.GetMouseButtonDown(0))
//{
// if(obj.gameObject.activeSelf == true)
// {
// obj.gameObject.SetActive(false);
// }
// else
// {
// obj.gameObject.SetActive(true);
// }
//}
Debug.DrawRay(new Vector3(0,1,0), new Vector3(0,5,0),Color.blue);
}
GameObject insObj;
void RayPoint()
{
if (Input.GetMouseButtonDown(1))
{
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rHit;
if (Physics.Raycast(r, out rHit, 100))
{
Instantiate(obj.gameObject, rHit.point, Quaternion.Euler(0, 0, 0));
print(rHit.collider.gameObject.name); //부딪힌 오브젝트의 이름 나오기
}
}
}
//이동하는 함수
void Move()
{
if (Input.GetKey(KeyCode.UpArrow))
{
this.gameObject.transform.Translate(0, 0, _moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow))
{
this.gameObject.transform.Translate(0, 0, -_moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
this.gameObject.transform.Translate(-_moveSpeed * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
this.gameObject.transform.Translate(_moveSpeed * Time.deltaTime, 0, 0);
}
}
//점프하는 함수
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 0)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 1)
{
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * _addforce);
_jumpNum++;
}
else if (Input.GetKeyDown(KeyCode.Space) && _jumpNum == 2)
return;
}
//카메라 시점변경
void See()
{
if (Input.GetKeyDown(KeyCode.V))
{
if (cameraNum == 0)
{
Camera.main.transform.position = cameraTr[1].transform.position;
Camera.main.transform.rotation = cameraTr[1].transform.rotation;
cameraNum = 1;
}
else if (cameraNum == 1)
{
Camera.main.transform.position = cameraTr[0].transform.position;
Camera.main.transform.rotation = cameraTr[0].transform.rotation;
cameraNum = 0;
}
}
}
void OnCollisionEnter(Collision collision)
{
_jumpNum = 0;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Obstacle")
{
print("부딪힘!!");
}
if (other.tag == "Goal")
{
print("종료");
}
}
}
댓글남기기