using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
Rigidbody rigid;
public float jumpPower;
public float speed;
public int hp;
private void Start()
{
rigid = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetButtonDown("Jump"))
{
rigid.velocity = new Vector3(0, jumpPower, 0); // 플레이어의 점프
}
}
private void OnCollisionEnter(Collision collision) // 충돌 감지
{
if (collision.gameObject.tag == "Wall")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
GetButtonDown("Jump") 함수를 이용해 키 입력을받는다.
Jump 키워드의 경우
Project Settings > input Manager > Jump 부분에 이미 정해져 있다.
Positive Button 의 값이 'space'로 이미 정해진것을 확인 할 수 있다.
OnCollisionEnter(Collision collision)함수를 이용해 충돌을 감지한다.
이 함수는 스크립트가 부착된 오브젝트와 이 외의 오브젝트와의 충돌을 감지하는데 사용된다.
위의 경우 tag를 이용해 tag가 Wall인 물체와의 충돌만을 감지하게 하였다.
플레이어가 wall물체와 충돌할 경우 LoadScene을 호출해 씬을 재시작한다.
SceneManager.GetActiveSvene().naem 을 이용해 현제 액티브된 씬의 이름을 가져온다.
SceneManager.LoadScene("Game");
LoadScene의 인자값으로 씬의 이름을 직접 적어주어도 동일한 기능을 수행한다.
생성된 오브젝트의 특성들과 성질을 기억하는 재사용이 가능한 오브젝트
장애물 소환은 Spawner 오브젝트를 새로 생성해 스크립트를 추가해준다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject wallPrefab;
public float spawntime = 1.5f;
float term;
public float range = 3;
private void Start()
{
term = spawntime;
}
private void Update()
{
term += Time.deltaTime;
if(term> spawntime)
{
Vector3 pos = transform.position;
pos.y += Random.Range(-range, range);
Instantiate(wallPrefab , pos, transform.rotation);
term -=spawntime;
}
}
}
Instantiate함수를 이용해 오브젝트를 새로 생성해준다.
Instantiate(프리펩,vector3,rotation)의 값을 인자로 가진다.
private void Start()
{
rigid = GetComponent<Rigidbody>();
textMesh = GameObject.Find(name :"Score").GetComponent<TextMesh>();
}
player의 스크립트에서 textMesh를 GameObject.Find를 이용해 textMesh를 찾아준다.
public void addSocre(int s)
{
score +=s;
textMesh.text = "점수 : " + score;
}
이를 이용해 점수를 추가해주는 addScore 함수를 만든다.
점프 파워 변경 로직
public void boostJump()
{
if(transform.position.y <-2)
{
jumpPower = 7;
}
else
{
jumpPower = 5;
}
플레이어의 y 값이 -2이하가 된 경우 점프 파워를 변경해준다.
transform.Translate(Vector3.right * speed * Time.deltaTime);
플레이어가 앞으로 조금씩 이동하도록 변경해보았다.
transform.localScale += Vector3.up*Time.deltaTime*0.1f;
플레이어의 x축 크기가 점점 증가하게 하였다.
public GameObject[] wallPrefab;
Instantiate(wallPrefab[randomRange],transform.position,transform.rotation);
여러 형태의 장애물 생성