Post

Unity(5)

Unity(5) Item 생성 및 기능 구현


Item 생성 및 회전시켜보기기:

  1. Hierarcy 창에서 우클리 후 cube 생성하기, 이름 Item으로 변경(기울기, 크기, matarial 변경해보기).
  2. Item을 코드를 통해 회전시키기 위해서 아래의 과정 진행.
  3. project창 script 폴더에 ItemMovement이름으로 Script생성하기.
  4. 아래의 코드 스크립트에 추가하기.

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemMovement : MonoBehaviour
{
    // Start is called before the first frame update
    public float rotSpeed = 100f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0,rotSpeed *Time.deltaTime,0)); //Time.deltaTime을 곱하는 이유는 모든 컴퓨터에서 똑같이 움직이게 하기 위해서이다.
    }
}
  1. 변경된 script를 Item의 컴포넌트에 추가하기.
  2. 실행하면 움직이는 y축을 기준으로 돌아가는 것을 볼 수 있다.

screen</img>

Item 기능(player와 만났을 때 사라지고 카운트 세기) 구현해보기

  1. 우선 Item의 Collider에 있는 “IsTrigger”옵션을 킨다.
    • IsTrigger의 기능은 부딪혔을 시, 충돌판정이 아니라 닿았다는 것만 확인한다는 뜻인거 같다.
  2. PlayerMovement 스크립트에서 획득한 item의 개수를 세는 변수를 생성.
  3. 아래의 코드를 추가하여 접촉시 count를 증가시키고, item은 사라지게 설정.

code:

1
2
3
4
5
6
private void OnTriggerEnter(Collision other){
    if(other.tag == "Item"){ //만약 tag가 item인 물체를 만나면
        itemCount++; //count는 증가시키고
        Destroy(other.gameObject);//닿은 물체는 없앤다.
    }
}
  1. 위의 코드는 tag를 사용한다. tag는 밑에 사진처럼 add tag를 통해 만들어줄 수있다. Item tag를 만들어 적용하면 된다.

screen</img>

위와 같이 변경해주고 실행해보면 player와 접촉시 count는 올라가고 item 모양은 사라지는 것을 알 수 있다.«br>

screen</img>

PlayerMovent 전체코드:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{   
    public float speed;
    public float jumpPower;
    public int itemCount; //획득한 item 개수수 
    bool isJump; //현재 점프 상대흫 나타내는 boolean, 땅에 닿고 있으면 false
    private Rigidbody rigid;
    // Start is called before the first frame update
    void Start()
    {   
        itemCount = 0;
        isJump = false;
        rigid = GetComponent<Rigidbody>();  //내컴포넌트에서 Rigidbody가져오기기
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");//좌우 방향키
        float v = Input.GetAxisRaw("Vertical"); //상하 방향키

        rigid.AddForce(new Vector3(h,0,v) * speed, ForceMode.Impulse);// rigidbody에 힘을 전해주는 함수

        if(Input.GetKeyDown(KeyCode.Space) && !isJump){
            isJump = true;
            rigid.AddForce(new Vector3(0,jumpPower,0), ForceMode.Impulse);
        }
    }

    private void OnCollisionEnter(Collision other){
        if(other.gameObject.name == "floor"){
            isJump = false;
        }
    }

    private void OnTriggerEnter(Collider other){
        if(other.tag == "Item"){
            itemCount++;
            Destroy(other.gameObject);
        }
    }
}

ItemMovement 전체코드:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemMovement : MonoBehaviour
{
    // Start is called before the first frame update
    public float rotSpeed = 100f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0,rotSpeed *Time.deltaTime,0));
    }
}

감사합니다.

This post is licensed under CC BY 4.0 by the author.