public class PlayerController : MonoBehaviour {
[HideInInspector]
public bool isFacingRight = true; //스프라이트를 방향에 맞게 뒤짚기위한 방향 변수
[HideInInspector]
public bool isJumping = false; //점프 여부를 나타내는 변수.
[HideInInspector]
public bool isGrounded = false; //지면에 닿아있는지 여부를 체크하는 변수
public float jumpForce = 250.0f; //점프의 힘
public float maxSpeed = 4.0f; //이동시 최고 속도의 값
public PhysicsMaterial2D jumpMaterial; // 이 부분이 뭔지 모르겠네요..
public Transform groundCheck; //게임 오브젝트가 지면에 닿아있는지 체크하는 프로퍼티
public LayerMask groundLayers; //게임오브젝트가 지면에 있는지 알 수 있는 레이어 리스트 라고 나와있는데 이 두개의 차이, 무슨 역할인지 이해가 안가네요..
private float groundCheckRadius = 0.2f; //groundCheck의 가운데로부터 떨어진 거리 라는데 이것도 이해가...
private Animator anim; //애니메이터형의 anim선언하고
void Awake()
{
anim = this.GetComponent<Animator>(); //그 변수 안에 애니메이터 컴포넌트 변수를 넣고.
}
// Use this for initialization
// Update is called once per frame
void Update ()
{
if (GetComponent<Rigidbody2D>().velocity.y == 0)
isGrounded = true;
if (Input.GetButtonDown("Jump"))
{
if (isGrounded == true)
{
this.GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
this.anim.SetTrigger("Jump");
}
else if (isJumping == false)
{
isJumping = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayers);
PhysicsMaterial2D material = this.gameObject.GetComponent<CircleCollider2D>().sharedMaterial;
if (isGrounded)
{
isJumping = false;
}
if (isGrounded == true && material == this.jumpMaterial)
{
CircleCollider2D collision = this.gameObject.GetComponent<CircleCollider2D>();
collision.sharedMaterial = null;
collision.enabled = false;
collision.enabled = true;
}
else if (isGrounded == false && this.gameObject.GetComponent<CircleCollider2D>().sharedMaterial == null)
{
CircleCollider2D collision = this.gameObject.GetComponent<CircleCollider2D>();
collision.sharedMaterial = this.jumpMaterial;
collision.enabled = false;
collision.enabled = true;
}
try
{
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
this.anim.SetFloat("Speed", Mathf.Abs(move));
if ((move > 0.0f && isFacingRight == false) || (move < 0.0f && isFacingRight == true))
{
Flip();
}
}
catch (UnityException error)
{
Debug.LogError(error.ToString());
}
}
void Flip()
{
isFacingRight = !isFacingRight;
Vector3 playerScale = transform.localScale;
playerScale.x = playerScale.x * -1;
transform.localScale = playerScale;
}
}
하나하나 물어보려고 velocity 에대해서만 물어봤었는데.. 다른것도 좀 이해안가는게 있어서.. 간단하게나마 설명 해줄 용자 ..
내가 해줄수 있긴 한데... 근데 이거 어디서 가져왔냐? 돌아는 가겠지만 코드가 좀 이상함
책 예제에 있는 스크립트임.. 나도 뭔가 이상한거같기도 하고해서 올려봄.. 후반에 가면 더 추가되거나 수정되는부분잊있을수도 우선 이상한부분은 어느부분이 왜이상한지는 안알려주더라도 저기 있는 함수들이 무슨역할인지 그리고 어떤흐름인지만 대략 알려줘
지금 대강 다 써감 한 5분만 기다려라
ㄴ 땡큐~~