Unity

러닝게임 소스

lipnus 2018. 10. 5. 20:34
반응형




Character

 

using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class Charactor : MonoBehaviour {

    public float moveSpeed = 2f;
    public float jumpPower = 300f;

    public GameManager gameManager;

    // Use this for initialization
    void Start () {
        Time.timeScale = 1f;
    }
    
    // Update is called once per frame
    void Update () {

        //Update프레임 시간차 * Time.Timescale = Time.deltaTime
        transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
    }

    public void Jump(){
        Debug.Log("Jump()");
        GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumpPower);
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("충돌");
        if(col.transform.name== "WinCollider"){
            gameManager.Win();
        }else if(col.transform.name=="LoseCollider"){
            gameManager.Lose();
        }    
    }


}






GameManager


using System.Collections;

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


public class GameManager : MonoBehaviour {

    public GameObject paneObj;
    public GameObject paneObjText;

    public void Win(){
        paneObj.SetActive(true);
        paneObjText.GetComponent<Text>().text = "Win";
        Time.timeScale = 0f; //일시정지
    }


    public void Lose(){
        paneObj.SetActive(true);
        paneObjText.GetComponent<Text>().text = "Lose";
        Time.timeScale = 0f; //일시정지
    }

    public void Replay(){
        //현재의 Scene을 실행
        SceneManager.LoadScene("PlayScene");
    }
}






BackgroundManager


using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class BackgroundManager : MonoBehaviour {

    public GameObject cloud;
    public GameObject building;
    public GameObject near;
 
    
    // Update is called once per frame
    void Update () {

        cloud.transform.Translate(Vector3.right * 3.2f * Time.deltaTime);
        cloud.transform.localScale += new Vector3(0, 0.01f * Time.deltaTime, 0);

        building.transform.Translate(Vector3.right * 2f * Time.deltaTime);
        near.transform.Translate(Vector3.left * 1f * Time.deltaTime);
    }
}






반응형

'Unity' 카테고리의 다른 글

[Animation] 점프  (0) 2018.10.07
화살쏘기  (0) 2018.10.07
UI 텍스트 변경  (0) 2018.10.03
충돌체크 시 회전하지 않게 고정하기  (0) 2018.10.03
GameObject 불러오는 방법  (0) 2018.10.03