골드메탈님 유니티 게임 개발 강의로 간단하게 게임 만들고 있었는데 실행하기만 하면 컴퓨터가 터져서 여기에 물어봄.

간단하게 마리오같은 겜인데 골드메탈님 유니티 기초 강좌 #b13에서 만드는 게임임.(다만, 코드 배끼기만 하면 실력이 안 늘 것 같아서, 코드 조금 바꾼 정도임.)

어쨌든, 이거 진짜 순수하게 컴퓨터 성능이 딸려서 일까?ㅠㅠ



#1:주인공(플레이어가 조종하는 객체) 스크립트


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


public class Move : MonoBehaviour
{
  
    Rigidbody2D rigid;
    public float max_speed;
    bool isjump;
    SpriteRenderer Spriterenderer;
    Animator Animator;
    float speed;




    // Start is called before the first frame update
    void Awake()
    {
       
        isjump = false;
        max_speed = 10;
        rigid = GetComponent<Rigidbody2D>();
        Spriterenderer = GetComponent<SpriteRenderer>();
        Animator = GetComponent<Animator>();
    }

     void Update()
    {
       
       
       
       
        if(Input.GetButton("Horizontal") || Input.GetButtonDown("Horizontal"))                      //a,d 키 조정
        {
            speed = Input.GetAxisRaw("Horizontal") * 4;

            rigid.AddForce(Vector2.right * speed , ForceMode2D.Impulse);
                Animator.SetInteger("condition", 2);
         

            if (rigid.velocity.x > max_speed)               // 움직이는 거 속도 조절
            {
                rigid.velocity = new Vector2(max_speed, rigid.velocity.y);
            }
            else if (rigid.velocity.x < max_speed * -1)
            {
                rigid.velocity = new Vector2(max_speed * -1, rigid.velocity.y);

            }

            Spriterenderer.flipX = Input.GetAxisRaw("Horizontal") == -1;

        }




        if (Input.GetButtonUp("Horizontal"))
        {
            if (isjump == false )
            anime(1);
          
        }




    }

    // Update is called once per frame
    void FixedUpdate()
    {

       

        if (!isjump)
        {
            float jump = Input.GetAxisRaw("Jump");
            rigid.AddForce(Vector2.up * jump * 30, ForceMode2D.Impulse);
           


            if (Input.GetAxisRaw("Jump") != 0f)
            {
                anime(3);
                StartCoroutine("Jumpcooltime");
            }


        }
    }
      

    IEnumerator Jumpcooltime() 
    {
        yield return new WaitForSeconds(0.1f);
      
        isjump = true;
        yield return new WaitForSeconds(2f);
        anime(1);
        isjump = false;
       


    }



  void anime(int target)  //애니매이션
    {                              
        switch (target)
        {
            case 1:  //print("가만히 있기");
                Animator.SetInteger("condition", 1);                                      //stay1
                break;
            case 2: //print("걷기");
                Animator.SetInteger("condition", 2);                                  
                break;
            case 3: // print("점프하기");
                Animator.SetInteger("condition", 3);                                     //walk2
                break;
                                           
              
                                                                                             // jump3
             
                                        
              




        }


    }
}

#2: 코인 스크립트

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

public class coin : MonoBehaviour
{
   

    private void Start()
    {
       
      
    }



    private void OnCollisionEnter2D(Collision2D collision)
    {
        fail_decider.Coin += 1;
        game.SetActive(false);
    }
    
       
   







}



#3: 가시(닿으면 주인공 죽고 스테이지 제시작) 장애물 스크립트

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

public class fail2 : MonoBehaviour
{
  

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.game.name == "Player_0")
        {
            fail_decider.Coin = 0;
            SceneManager.LoadScene(finalflag.stage_code);
        }
    }
}


#4: 몬스터(마리오의 굼바같은 느낌?) 스크립트

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

public class monster_ai : MonoBehaviour
{
    int chance = 5;
    Rigidbody2D rigid;
    SpriteRenderer spriterenderer;



    // Start is called before the first frame update
    void Awake()
    {
         rigid = GetComponent<Rigidbody2D>();
        spriterenderer = GetComponent<SpriteRenderer>();
    }





    // Update is called once per frame
    void Update()
    {
        Debug.DrawRay(new Vector2(transform.position.x, transform.position.y), Vector2.up*2, new Color(0, 1, 0));
        StartCoroutine("Notice");
    }





    private IEnumerator Notice()
    {
        int max_speed = 3;
        int rotate= 1;
       if(spriterenderer.flipX == true)
        {
            rotate = 1;
          

        }
        if (spriterenderer.flipX == false)
        {
            rotate = -1;


        }

        rigid.AddForce(Vector2.right * rotate, ForceMode2D.Impulse);
        chance--;
        if (rigid.velocity.x > max_speed)
        {
            rigid.velocity = new Vector2(max_speed, rigid.velocity.y);
        }
        else if (rigid.velocity.x < max_speed* -1 )
        {
            rigid.velocity = new Vector2(max_speed * -1, rigid.velocity.y);
        }

        if (chance == 0)
        {
            yield return new WaitForSeconds(1);
            spriterenderer.flipX = rotate == -1;   // true면 false로 false면 true로
            chance = 5;
           
        }
       



    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
       
         RaycastHit2D raycast = Physics2D.Raycast(new Vector2 (transform.position.x, transform.position.y), Vector2.up * 2, 2, LayerMask.GetMask("player"));
        if (collision.game.name == "Player_0"&& raycast.collider == null)
        {
            SceneManager.LoadScene(finalflag.stage_code);
            fail_decider.Coin = 0;

        }
        if (raycast.collider != null)
        {
            game.SetActive(false);

        }
    }
}


#5:코인 얼마나 얻었는지 확인해주는 ui 스크립트

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

public class coincount_ui : MonoBehaviour
{
   
    public TextMeshProUGUI coincount;
  
    void Awake()
    {
        coincount = GetComponent<TextMeshProUGUI>();
       
    }

    // Update is called once per frame
    void Update()
    {
        if (coincount != null) {
        coincount.text = ""+ fail_decider.Coin;
        }
        if (coincount == null)
        {
            Debug.Log("bug!");
        }

    }
}


#6:아래로 주인공이 떨어진다면 다시 시작하게 해주고, 겸사겸사 static 변수로 코인 얼마나 있는지 저장해놓음

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




public class fail_decider : MonoBehaviour
{
   
   
     static public int Coin ;
   


    private void OnCollisionEnter2D(Collision2D collision)  //낭떨어지 탈락
    {
        if(collision.game.name == "Player_0")
        {
            Coin = 0;
            SceneManager.LoadScene(finalflag.stage_code);

        }
    }

   
}

#7: 결승점 오브젝트 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Threading;

public class finalflag : MonoBehaviour
{
    static public int stage_code = 1;
  
   
    private void OnCollisionEnter2D(Collision2D collision) //scene 넘어가기
    {
      
        if (collision.game.name == "Player_0")
        {
            // SceneManager.LoadScene(1);
            stage_code++;
            SceneManager.LoadScene(stage_code);

        }
    }
}






- dc official App