박하의 나날

(07.27)Quaternion_centreeGun

프로그래밍/Unity

Quaternion을 이용한 센트리건.

일정범위에 들어가면 경고등이 켜지고 총알 발사... 지만 경고등까지만 .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using UnityEngine;
using System.Collections;
 
public class move : MonoBehaviour {
    float moveSpeed = 5f;
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
 
        transform.Translate(new Vector3(00, v) * moveSpeed * Time.deltaTime);
        transform.Rotate(new Vector3(0, h, 0* moveSpeed*10f * Time.deltaTime);
    }
}
 
cs

일단 키보드로 움직이게 해주고.

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
using UnityEngine;
using System.Collections;
 
public class centree : MonoBehaviour {
    public GameObject target;
    public Light L;
 
    void Start()
    {
        L = GameObject.Find("warningLight").GetComponent<Light>();
    }
    void Update()
    {
        if (target == nullreturn;
 
 
        Vector3 vec = target.transform.position - transform.position;
        Quaternion q = Quaternion.LookRotation(vec);
        Quaternion s = Quaternion.Slerp(transform.rotation, q, 10f * Time.deltaTime);
 
        transform.rotation = s;
 
        L.intensity += 0.2f;
        if (L.intensity > 7f) L.intensity = 7f;
 
        if (Vector3.Distance(target.transform.position, transform.position) > 10f)
        {
            L.intensity = 0f;
            target = null;
            Quaternion p = Quaternion.LookRotation(transform.forward);
            transform.rotation = p;
        }
    }
}
 
cs

경고등 warning Light. 일정범위에 타겟이 들어오고 나가면 밝기가 조절된다.

 

LookRotation으로 타겟을 바라보고 Slerp으로 부드럽게.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using UnityEngine;
using System.Collections;
 
public class trgger : MonoBehaviour {
    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.name == "player")
        {
            Debug.Log("player Enter");
            GameObject gun = GameObject.Find("centreeGun");
            gun.GetComponent<centree>().target = col.gameObject;
        }
    }
}
 
cs

벽 사이에 box collider를 두고 trigger로 사용.

 

 

'프로그래밍 > Unity' 카테고리의 다른 글

NGUI_아틀라스 생성  (0) 2017.04.06
(07.31)raycast_Picking  (0) 2017.03.20
(07.27)Quaternion_유도미사일, 쫓아다니는 카메라  (0) 2017.03.20
(07.23)mecanim, animator  (0) 2017.03.20
(07.21)joint_2  (0) 2017.03.20