본문 바로가기
College Computer Science/VR AR MR

[Unity] 다이빙 게임 (팀프로젝트)

by 2den 2022. 2. 15.
728x90

PlayerController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    private Vector2 m_Move;
    private bool is_parachute;
    private Rigidbody rb;
    private AudioSource ParaAudio;
    private AudioSource WindAudio;
    private AudioSource RipAudio;
    private AudioSource ItemAudio;
    private int para_num = 0;
    private bool is_invincible; // 절대무적
    private float hori_resist = 0.3f;
    private EventSystem es;
    private float bug_fail_timer;

    public Vector2 left_con_pos;
    public Vector2 right_con_pos;

    public float force=1f;
    public float rotate_force = 1f;
    public GameObject ev;
    public GameObject ParaEffect;
    public GameObject WindEffect;
    public GameObject RipEffect;
    public GameObject ItemEffect;
    // Start is called before the first frame update
    void Start()
    {
        is_parachute = false;
        is_invincible = false;
        rb = gameObject.GetComponent<Rigidbody>();
        ParaAudio = ParaEffect.GetComponent<AudioSource>();
        WindAudio = WindEffect.GetComponent<AudioSource>();
        RipAudio = RipEffect.GetComponent<AudioSource>();
        ItemAudio = ItemEffect.GetComponent<AudioSource>();
        es = ev.GetComponent<EventSystem>();
        WindAudio.Play();
        bug_fail_timer = 0;
    }

    public void OnMove(InputValue value)
    {

        m_Move = value.Get<Vector2>();

    }

    public void OnFire()
    {
        Debug.Log("fire");
        if (para_num > 0 && !(is_parachute))
        {

            para_num--;
            is_parachute = true;
            ParaAudio.Play();
            Debug.Log("para open");
        }
        else if (is_parachute) {

            is_parachute = false;
            RipAudio.Play();
        }
    }

    public void OnEscape()
    {
        es.Over();
    }



    // Update is called once per frame
    void FixedUpdate()
    {
        WindAudio.volume = (Mathf.Abs(rb.velocity.y)+10) / 70;
        if (left_con_pos != new Vector2(0, 0) || right_con_pos != new Vector2(0, 0)) {

            m_Move = left_con_pos + right_con_pos;

        }


        rb.AddForce(transform.forward * force * m_Move.y-new Vector3(rb.velocity.x, 0,rb.velocity.z)* hori_resist);

        



        if (is_parachute)
        {
            rb.AddForce(0, -rb.velocity.y, 0);
        }
        else {
            rb.AddForce(0, -rb.velocity.y*0.1f, 0);
        }



        transform.Rotate(new Vector3(0, 1f* m_Move.x, 0));
        //transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(0, m_Move.x * rotate_force * Time.deltaTime, 0) );


        //버그방지
        if (transform.position.y < 4f) {
            bug_fail_timer += Time.deltaTime;
            if (transform.position.y < 2f)
            {
                es.Fail();
                transform.position = new Vector3(transform.position.x, 2, transform.position.z);
            }

            if (bug_fail_timer > 3f) {

                es.Fail();
            }
        }
        
    }


    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.gameObject.tag);

        if (other.gameObject.CompareTag("Parachute"))
        {
            para_num++;
            es.Point(100);
            ItemAudio.Play();
            other.gameObject.SetActive(false);
        }
        else if (other.gameObject.CompareTag("Point"))
        {
            es.Point(200);
            ItemAudio.Play();
            other.gameObject.SetActive(false);
        }
        else if (other.gameObject.CompareTag("Invincible"))
        {
            es.Point(100);
            is_invincible = true;
            ItemAudio.Play();
            other.gameObject.SetActive(false);
        }
        else if (other.gameObject.CompareTag("Obstacle"))
        {
            if (!is_invincible)
            {
                es.Fail(); // not clear not over but fail
            }
            else
            {
                is_invincible = false;
            }
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Clear")&& is_parachute)
        {

            es.Clear();

        }
        else
        {

            es.Fail();
        }
    }

}

 

 

EventSystem.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EventSystem : MonoBehaviour
{
    public RawImage[] Ri_clear, Ri_fail;
    public Text[] text;
    private bool is_clear;
    private bool is_fail;
    private int score;
    private float timer;

    private AudioSource audio;


    private void Final_score() {

        score+=(int)(5000f / timer);
    }

    public void Clear() {
        Final_score();
        is_clear = true;
    }

    public void Over()
    {
        #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
        #else
            Application.Quit();
        #endif
    }

    public void Fail()
    {
        if (!is_clear)   is_fail = true;
    }

    public void Point(int point=100) {

        score += point;
    }

    // Start is called before the first frame update
    void Awake()
    {
        is_clear = false;
        is_fail = false;
        for (int t = 0; t < Ri_clear.Length; t++)   Ri_clear[t].color = new Color(1f,1f,1f,0);
        for (int t = 0; t < Ri_fail.Length; t++)    Ri_fail[t].color = new Color(1f, 1f, 1f, 0);
        audio = gameObject.GetComponent<AudioSource>();
        score = 0;
        timer = 0f;
    }

    private void Update()
    {
        if (!is_fail)
        {

            for (int t = 0; t < text.Length; t++)
            {
                text[t].text = "Score: " + score.ToString() + "\n";
                text[t].text += "Time Bonus: " + (int)(5000f / timer);
            }

        }
        else {

            for (int t = 0; t < text.Length; t++)
            {
                text[t].text = "";
            }
        }
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (!is_clear&&!is_fail) {
            timer += Time.deltaTime;
        }
            
        if (is_clear) {

            for (int t = 0; t < Ri_clear.Length; t++) {
                if (Ri_clear[t].color.a < 1)
                {

                    Ri_clear[t].color += new Color(0, 0, 0, 1 * Time.deltaTime);
                }
            }
            
        }

        if (is_fail)
        {
            for (int t = 0; t < Ri_fail.Length; t++) {
                if (Ri_fail[t].color.a < 1)
                {

                    Ri_fail[t].color += new Color(0, 0, 0, 1 * Time.deltaTime);
                }

            }
                
        }

    }
}

 

 

ControllerActions.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class ConrollerAction : MonoBehaviour
{
    public SteamVR_Input_Sources handType;
    public SteamVR_Action_Boolean trigger;
    public GameObject player;
    private Vector3 height_offset;
    private bool is_pull=false;

    // Start is called before the first frame update
    void Awake()
    {

        height_offset = new Vector3(0, 1, 0);
        player = GameObject.Find("player");
    }

    // Update is called once per frame
    void Update()
    {
        if (GetGrip())
        {
            if (!is_pull)
            {
                is_pull = true;
                player.GetComponent<PlayerController>().OnFire();
            }
        }
        else {

            is_pull = false;
        }
        if (handType == SteamVR_Input_Sources.LeftHand)
        {

            player.GetComponent<PlayerController>().left_con_pos =
                this.gameObject.transform.localPosition + this.transform.parent.localPosition - height_offset;
        }
        else if (handType == SteamVR_Input_Sources.RightHand) {

            player.GetComponent<PlayerController>().right_con_pos = 
                this.gameObject.transform.localPosition + this.transform.parent.localPosition - height_offset;
        }
    }

    public bool GetGrip() {

        return trigger.GetState(handType);
    }
}

 

 

ForPlanes.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ForPlanes : MonoBehaviour

{
    public float speed;
    public float radius;
    public float start_rotation = 0;
    private float runningTime = 0;
    private Vector3 origin;

    // Start is called before the first frame update
    void Start()
    {
        origin = gameObject.transform.localPosition;
        runningTime = Mathf.Deg2Rad * start_rotation;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        runningTime += Time.deltaTime * speed;
        Vector3 moveDir = new Vector3(radius * Mathf.Cos(runningTime), 0.0f, radius * Mathf.Sin(runningTime))+ origin;
        Vector3 rot = new Vector3(0, -Mathf.Rad2Deg * runningTime+90, 0);
        transform.localPosition=moveDir;
        //transform.Translate(moveDir);
        transform.rotation = Quaternion.Euler(rot);
    }
}

 

 

CameraPostFix.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraPosFix : MonoBehaviour
{
    private GameObject camera;
    // Start is called before the first frame update
    void Awake()
    {
        for (int k = 0; k < this.gameObject.transform.childCount; k++) {

            GameObject go = this.gameObject.transform.GetChild(k).gameObject;

            if (go.name == "Camera") {

                camera = go;
                break;
            }
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        this.gameObject.transform.localPosition = -camera.transform.localPosition;
    }
}

 

 

발표 자료 :

 

201012_personal_br_sby

Skydiving Game from Kingsman

docs.google.com

 

 

프로젝트 코드 :

 

GitHub - TelAr/freedom_diver: team_project_for_VR

team_project_for_VR. Contribute to TelAr/freedom_diver development by creating an account on GitHub.

github.com

 

 

 

728x90

'College Computer Science > VR AR MR' 카테고리의 다른 글

[VR] BCI(Brain-Computer Interface)와 VR  (0) 2022.02.15
[Unity] 네모로직 3D  (0) 2022.02.15
[Unity] Roll a Ball  (0) 2022.02.15

댓글