지난 시간에 캐릭터가 넘어지는 오류 아닌 오류를 보았습니다. z 값이 수정되지 않도록 코드를 수정해볼까요?

캐릭터가 뒤집어지는 순간을 살펴보면 충돌할때 벌어지는 경우를 발견할 수 있습니다. PlayerController 스크립트에 다음과 같이 코드를 추가하여 캐릭터의 각도를 초기화 해주면 충돌시 변경되는 각도가 다시 초기화 됩니다. 다만 캐릭터의 움직임이 자연스러울려면 좀더 고민을 해야 할 듯 합니다. 오늘도 수고하셨습니다~~
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public static PlayerController instance1;
public float speed = 10.0f;
private Animator animator;
void Awake(){
instance1 = this;
// Debug.Log("Awake");
}
void Start()
{
animator = GetComponent<Animator>();
// Debug.Log(GameManager.instance.GameScore);
}
// 충돌이 시작되는 순간 호출
void OnCollisionEnter2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
print("충돌 Enter~");
if(GameManager.instance.GameScore > 0){
GameManager.instance.GameScore -=1;
}
Debug.Log(GameManager.instance.GameScore);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
if (other.collider.tag == "Coin") {
print("충돌 Enter~");
GameManager.instance.GameScore +=1;
Debug.Log(GameManager.instance.GameScore);
Destroy(GameObject.FindGameObjectWithTag("Coin"), 0.1f);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
}
// 충돌이 되고 있을 때 매 프레임마다 호출
void OnCollisionStay2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
// print("충돌 Stay~");
}
}
// 충돌이 끝날 때 호출
void OnCollisionExit2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
// print("충돌 Exit~");
}
}
void Update(){
if(Input.GetAxisRaw("Horizontal") < 0 || Input.GetAxisRaw("Horizontal") > 0){
transform.Translate(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, 0, 0);
transform.localScale = new Vector2(Input.GetAxisRaw("Horizontal"), 1);
animator.SetBool("RunStart", true);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
} else if(Input.GetAxisRaw("Horizontal") == 0) {
animator.SetBool("RunStart", false);
}
if (Input.GetAxisRaw("Vertical") > 0 || Input.GetKey(KeyCode.Space)) {
transform.Translate(0, speed * Time.deltaTime, 0);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
}
}

지난 시간에 캐릭터가 넘어지는 오류 아닌 오류를 보았습니다. z 값이 수정되지 않도록 코드를 수정해볼까요?
캐릭터가 뒤집어지는 순간을 살펴보면 충돌할때 벌어지는 경우를 발견할 수 있습니다. PlayerController 스크립트에 다음과 같이 코드를 추가하여 캐릭터의 각도를 초기화 해주면 충돌시 변경되는 각도가 다시 초기화 됩니다. 다만 캐릭터의 움직임이 자연스러울려면 좀더 고민을 해야 할 듯 합니다. 오늘도 수고하셨습니다~~
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public static PlayerController instance1;
public float speed = 10.0f;
private Animator animator;
void Awake(){
instance1 = this;
// Debug.Log("Awake");
}
void Start()
{
animator = GetComponent<Animator>();
// Debug.Log(GameManager.instance.GameScore);
}
// 충돌이 시작되는 순간 호출
void OnCollisionEnter2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
print("충돌 Enter~");
if(GameManager.instance.GameScore > 0){
GameManager.instance.GameScore -=1;
}
Debug.Log(GameManager.instance.GameScore);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
if (other.collider.tag == "Coin") {
print("충돌 Enter~");
GameManager.instance.GameScore +=1;
Debug.Log(GameManager.instance.GameScore);
Destroy(GameObject.FindGameObjectWithTag("Coin"), 0.1f);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
}
// 충돌이 되고 있을 때 매 프레임마다 호출
void OnCollisionStay2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
// print("충돌 Stay~");
}
}
// 충돌이 끝날 때 호출
void OnCollisionExit2D(Collision2D other) {
if (other.collider.tag == "Enemy") {
// print("충돌 Exit~");
}
}
void Update(){
if(Input.GetAxisRaw("Horizontal") < 0 || Input.GetAxisRaw("Horizontal") > 0){
transform.Translate(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, 0, 0);
transform.localScale = new Vector2(Input.GetAxisRaw("Horizontal"), 1);
animator.SetBool("RunStart", true);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
} else if(Input.GetAxisRaw("Horizontal") == 0) {
animator.SetBool("RunStart", false);
}
if (Input.GetAxisRaw("Vertical") > 0 || Input.GetKey(KeyCode.Space)) {
transform.Translate(0, speed * Time.deltaTime, 0);
gameObject.transform.eulerAngles = new Vector3(0,0,0);
}
}
}