study/first_study/Assets/Player/PlayerController.cs

146 lines
3.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using TreeEditor;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rbody;
float axisH = 0.0f; // <20>Է<EFBFBD>
public float speed = 3.0f; // <20>̵<EFBFBD> <20>ӵ<EFBFBD>
public float jump = 9.0f;
public LayerMask groundLayer;
bool isDead = false;
bool onGround = false;
Animator animator;
public string stopAnime = "PlayerStop";
public string moveAnime = "PlayerMove";
public string jumpAnime = "PlayerJump";
public string goalAnime = "PlayerGoal";
public string deadAnime = "PlayerOver";
string nowAnime = "";
string oldAnime = "";
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
//Animator <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
animator = GetComponent<Animator>();
nowAnime = stopAnime;
oldAnime = stopAnime;
}
// Update is called once per frame
void Update()
{
axisH = Input.GetAxisRaw("Horizontal");
if(isDead)
{
return;
}
if(axisH > 0.0f) {
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>̵<EFBFBD>");
transform.localScale = new Vector2(1, 1);
}
else if (axisH < 0.0f)
{
Debug.Log("<22><><EFBFBD><EFBFBD> <20>̵<EFBFBD>");
transform.localScale = new Vector2(-1, 1);
}
}
private void FixedUpdate()
{
onGround = Physics2D.Linecast(transform.position,
transform.position - (transform.up * 0.1f),
groundLayer);
if (onGround || axisH != 0)
{
rbody.velocity = new Vector2(speed * axisH, rbody.velocity.y);
}
if (onGround)
{
Debug.Log("<22><><EFBFBD><EFBFBD>!");
Vector2 jumpPw = new Vector2(0, jump);
rbody.AddForce(jumpPw, ForceMode2D.Impulse);
}
if(onGround)
{
if(axisH == 0)
{
nowAnime = stopAnime;
}
else
{
nowAnime = moveAnime;
}
}
else
{
nowAnime = jumpAnime;
}
if(nowAnime != oldAnime)
{
oldAnime = nowAnime;
animator.Play(nowAnime);
}
}
public void Jump()
{
Debug.Log(" <20><><EFBFBD><EFBFBD> <20><>ư <20><><EFBFBD><EFBFBD>! ");
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.contacts[0].normal.y > 0.7f)
{
onGround = true;
}
}
private void OnCollisionExit2D(Collider2D collision)
{
onGround = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Goal")
{
Goal();
}
else if (collision.gameObject.tag == "Dead" && !isDead)
{
GameOver();
}
}
public void Goal()
{
animator.Play(goalAnime);
}
public void GameOver()
{
animator.Play(deadAnime);
}
}