728x90
오늘의 결과물
추가로 실행해본 것들
1. 움직이는 구름 추가
2. super 구름 추가 & super_jump 애니메이션 추가
3. 구름 자동 생성 & 소멸
4. 목숨 & Game out씬 추가
[전체 코드]
1. Game Scene
[main camera] : 오브젝트를 따라 main camera가 이동한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController2 : MonoBehaviour
{
GameObject player;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.Find("die");
}
// Update is called once per frame
void Update()
{
Vector3 playerPos = this.player.transform.position; //고양이의 좌표를 저장
transform.position = new Vector3(
transform.position.x, playerPos.y+1, transform.position.z); //고양이의 y좌표를 따라 카메라가 움직임
}
}
[player] : 플레이어(고양이)에 적용된 코드이자 전체를 감독하는 감독 스크립트 역할을 한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class playerController : MonoBehaviour
{
Rigidbody2D rigid2D;
BoxCollider2D BoxRigid2D;
CircleCollider2D CircleRigid2D;
float jumpForce = 680.0f;
float walkForce = 20.0f;
float maxWalkSpeed = 2.0f;
Animator animator;
GameObject camera;
GameObject cloud;
BoxCollider2D CollidCloud;
GameObject SuperCloud;
BoxCollider2D CollidSuperCloud;
int superN = 0;
GameObject[] cloudArray = new GameObject[7];
GameObject Gauge;
int hp = 3;
void Start()
{
this.rigid2D = GetComponent<Rigidbody2D>();
this.BoxRigid2D = GetComponent<BoxCollider2D>();
this.CircleRigid2D = GetComponent<CircleCollider2D>();
this.cloud = GameObject.Find("cloudPrefab (4)");
this.CollidCloud = cloud.GetComponent<BoxCollider2D>();
this.SuperCloud = GameObject.Find("superCloud");
this.CollidSuperCloud = SuperCloud.GetComponent<BoxCollider2D>();
this.camera = GameObject.Find("Main Camera");
this.Gauge = GameObject.Find("gauge2");
// this.animator.SetTrigger("walk");
for (int i = 0; i < 7; i++)
{
this.cloudArray[i] = GameObject.Find("cloudPrefab (" + i.ToString() + ")");
}
}
void Update()
{
//점프 구현 코드
if (Input.GetKeyDown(KeyCode.Space) && this.rigid2D.velocity.y == 0) //스페이스바를 누르면
{
this.rigid2D.AddForce(transform.up * this.jumpForce);
//오브젝트에 위로 힘을 가한다.(즉, 오브젝트는 점프한다.)
this.animator.SetTrigger("JumpTrigger"); //점프 애니메이션으로 변환.
}
//좌우 이동 구현 코드
int key = 0;
if (Input.GetKey(KeyCode.RightArrow)) key = 1; //오른쪽 이동
if (Input.GetKey(KeyCode.LeftArrow)) key = -1; //왼쪽 이동
float speedx = Mathf.Abs(this.rigid2D.velocity.x); //현재 오브젝트의 속력
if (speedx >= 0 && speedx < this.maxWalkSpeed) //속력 제한
{
this.rigid2D.AddForce(transform.right * key * this.walkForce); //좌우로 힘을 가해 오브젝트를 움직임
this.animator = GetComponent<Animator>(); //애니메이션
}
else if (speedx <= 0 && speedx > -this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce); //좌우로 힘을 가해 오브젝트를 움직임
this.animator = GetComponent<Animator>(); //애니메이션
}
//움직이는 방향에 따라 이미지 반전시키기
if (key != 0)
{
transform.localScale = new Vector3(key, 1, 1);
}
if (this.rigid2D.velocity.y == 0)
{
this.animator.speed = speedx / 2.0f; // 좌우로 이동하는 애니메이션
}
else
{
this.animator.speed = 1.0f; // 점프 애니메이션 속도
}
//화면 밖으로 나가는 경우
if (transform.position.y < -10)
{
hp -= 1;
this.Gauge.GetComponent<Image>().fillAmount -= 0.33f;
this.transform.position = new Vector3(-0.18f, -3.89f, 0);
if (hp == 0)
{
SceneManager.LoadScene("revive");
}
}
//super구름을 밟은 후 하강할 때
if (superN == 1 && this.rigid2D.velocity.y < -10) // super_jump
{
this.BoxRigid2D.isTrigger = false;
this.CircleRigid2D.isTrigger = false;
this.animator.SetTrigger("Super");
this.rigid2D.constraints = RigidbodyConstraints2D.FreezeRotation;
superN = 2;
}
// Debug.Log(camera.transform.position.y);
//슈퍼점프 애니메이션 종료
if (superN == 2 && Mathf.Abs(this.rigid2D.velocity.y) <= 0.5f)
{
this.animator.SetTrigger("walkk");
superN = 0;
}
if (superN == 0)
{
this.animator.SetTrigger("walkk");
}
Debug.Log(animator.GetCurrentAnimatorClipInfo(0)[0].clip.name);
//구름 오브젝트 생성 & 소멸
for (int i = 0; i < 7; i++)
{
if (camera.transform.position.y - 10 > cloudArray[i].transform.position.y)
{
int px = Random.Range(-2, 2);
float frontX = cloudArray[(i + 6) % 7].transform.position.x;
float frontY = cloudArray[(i + 6) % 7].transform.position.y;
float py = frontY + 2.0f;
while (px >= frontX - 0.7 && px <= frontX + 0.7
|| Mathf.Abs(px - frontX) > 2.8 | Mathf.Abs(px - frontX) < 0.3)
{
px = Random.Range(-2, 2);
}
cloudArray[i].transform.position = new Vector3(px,py, 0);
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
//깃발과의 충돌 판정
if (other.gameObject.tag == "flag")
{
Debug.Log("골");
SceneManager.LoadScene("ClearScene");
}
//superCloud충돌 판정
else if (other.gameObject.tag == "super")
{
//this.rigidCloud.isTrigger = true; // 구름의 trigger효과 활성화
this.BoxRigid2D.isTrigger = true;
this.CircleRigid2D.isTrigger = true;
this.rigid2D.AddForce(transform.up * this.jumpForce * 3 ); // 점프시킨다.
//this.rigid2D.constraints = RigidbodyConstraints2D.FreezePositionX;
superN = 1;
Debug.Log("super");
//superJump 애니메이션 효과
this.animator.SetTrigger("Super");
}
}
}
[movin_cloud] : 움직이는 구름을 위한 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cloudController : MonoBehaviour
{
Rigidbody2D rigid2D;
int onf = 0;
float force = 2000.0f;
// Start is called before the first frame update
void Start()
{
this.rigid2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(onf == 0)
{
this.rigid2D.AddForce(transform.right * 30.0f*force); //좌우로 힘을 가해 오브젝트를 움직임
Debug.Log("good");
onf = 1;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "leftWall")
{
this.rigid2D.AddForce(transform.right * 60.0f*force); //좌우로 힘을 가해 오브젝트를 움직임
}
else if (other.gameObject.tag == "rightWall")
{
this.rigid2D.AddForce(transform.right * - 60.0f*force);
}
}
}
2. Clear Scene
[Director] : 플레이어(고양이)가 정상까지 무사히 도착한 경우 마우스를 클릭하면 게임이 재시작 될 수 있도록 한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // 씬전환을 위한 함수 LoadScene을 사용하는 데 필요하다.
public class ClearDirector : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene("GameScene"); //마우스 왼쪽 버튼을 누르면 다시 게임 화면으로 전환된다.
}
}
}
3. Game out
[dieCloud] : Game out창 뒷 배경에 나오는 구름이 반복적으로 보이게 하기 위한 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dieCloud : MonoBehaviour
{
GameObject camera;
GameObject[] cloudArray = new GameObject[5];
// Start is called before the first frame update
void Start()
{
this.camera = GameObject.Find("Main Camera");
for (int i = 0; i < 5; i++)
{
this.cloudArray[i] = GameObject.Find("cloudPrefab (" + i.ToString() + ")");
}
}
// Update is called once per frame
void Update()
{
//구름 오브젝트 생성 & 소멸
for (int i = 0; i < 5; i++)
{
if (camera.transform.position.y + 5.5f < cloudArray[i].transform.position.y)
{
float py = camera.transform.position.y - 5.5f;
int px = Random.Range(-3, 3);
cloudArray[i].transform.position = new Vector3(px, py, 0);
}
}
}
}
[dieCamera] : Game out시 떨어지는 고양이를 Camera가 따라갈 수 있도록 하는 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
GameObject player;
// Start is called before the first frame update
void Start()
{
this.player = GameObject.Find("cat");
}
// Update is called once per frame
void Update()
{
Vector3 playerPos = this.player.transform.position; //고양이의 좌표를 저장
transform.position = new Vector3(
transform.position.x, playerPos.y, transform.position.z); //고양이의 y좌표를 따라 카메라가 움직임
}
}
위 게임을 제작해 나가는 자세한 순서를 알고싶다면 다음 링크로 이동해주세요!
[unity] class4_physics와 애니메이션_제작과정
bigballdiary.tistory.com
'대외 활동 > 유니티 스터디' 카테고리의 다른 글
[unity] class4_physics와 애니메이션_제작과정2 (0) | 2021.07.24 |
---|---|
[unity] class4_physics와 애니메이션_제작과정1 (0) | 2021.07.24 |
[unity] physics효과 주기 (collider, rigidbody, AddForce ...) (0) | 2021.07.22 |
[unity] class3_prefab과 충돌판정 (0) | 2021.07.22 |
[unity] class2_UI와 감독 스크립트 (0) | 2021.07.22 |