유니티2D 유투브 강의 메모


https://www.youtube.com/watch?v=BdlL5bwbCiI&list=PLNERyi31iYKRjJVreIz7Q5PsQ__VwTc9D


스프라이트 :

sprite(2D and UI)

sprite mode : multiple

sprite editor


*****좌우 움직임

moveX = Input.GetAxis("Horizontal");

if (moveX < 0.0f && facingRight == false){

FlipPlayer();

} else if (moveX > 0.0f && facingRight == true){

FlipPlayer();

}

gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);


*****캐릭터 플립

facingRight = !facingRight;

Vector2 localScale = gameObject.trasform.localScale;

localScale.x *= -1;

transform.localScale = localScale;


*****점프

GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);

if(Input.GetbuttonDown("Jump")){

Jump();

}


******이중 점프 방지

void OnCollisionEnter2D (Collision2D col){

if (col.gameObject.tag == "ground"){

isGrounded = true;

}

}



******카메라

player = GameObject.FindGameObjectWithTag("player");

float x = Mathf.Clamp (player.trasform.positon.x, xMin, xMax);

float y = Mathf.Clamp (player.trasform.positon.y, yMin, yMax);

gameObject.trasform.positon = new Vector3(x, y, gameObject.trasform.position.z);


******플레이어 죽음

hasDied = false;

if(gameObject.trasform.positon.y < -7) {

hasDied = trure;

}

if(hasDied == true){

StrartCorootine("Die");

}

IEnumerator Die(){

yield return new WaitForSeconds(2);

SceneManager.LoadScene("Main");

yield return null;

}


*****적 움직임

RaycastHit2D hit = Physics2D.Raycast(transform.positon, new Vector2(XmoveDirextion, 0));

gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(XMoveDirection, 0) * EnemySpeed;

if(hit.distance < 1.7f){

Flip();

}

if (XMoveDirection>0){

XMoveDrirection = -1;

}else{

XMoveDrirection = 1;

}



*****코인 먹기

void OnTriggerEnter2D (Collider2D trig){

if(trig.gameObject.name == "EndLevel"){

CountScore();

}

if(trig.gameObject.name == "Coin"){

playerScore += 10;

Destroy(trig.gameObject);

}

}


*****적 밟으면 점프

void PlyaerRaycast (){

RaycatHit2D hit = Physics2D.Raycast (trasnform.position, Vector2.down);

if(hit != null && hit.collider != null && hit.distance < 0.9f && hit.collider.tag == "enemy"){

GetComponent<Rigidbody2D>().AddForce(Vector2.up * 1000);

}

if(hit.distance < 0.9f && hit.collider.tag != "enemy"){

isGrounded = true;

}

}


































'개발 > 유니티' 카테고리의 다른 글

Debugging Unity Errors – Deterministic Compilation Failed  (0) 2022.01.20
유니티 2D 애니메이션  (0) 2019.03.03
OnMouseOver / OnMouseUp  (0) 2019.02.17
유니티 2D / 타일맵 (tilemap)  (0) 2018.10.22

+ Recent posts