유니티 기초
유니티 애니메이션(점프)
송현호
2023. 8. 3. 17:31
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControll : MonoBehaviour
{
public Animator anim;
private Rigidbody2D rBody2D;
public int jumpForce = 18;
public float moveSpeed = 1f;
public bool isJump = false;
public int state;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animator>();
this.rBody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if(state == 0)
{
this.rBody2D.velocity = new Vector2(rBody2D.velocity.x, jumpForce);
state = 1;
this.anim.SetInteger("State", state);
}
}
Move();
Debug.Log(this.rBody2D.velocity.y);
if (this.rBody2D.velocity.y < -0.2f)
{
state = 2;
this.anim.SetInteger("State", state);
}
}
public void Move()
{
float h = Input.GetAxisRaw("Horizontal");
if (h != 0)
{
this.transform.localScale = new Vector3(h, 1, 1);
}
this.rBody2D.velocity = new Vector2(moveSpeed * h, this.rBody2D.velocity.y);
if(state==0)
{
anim.SetFloat("Speed", Mathf.Abs(h));
}
}
public void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
if(state == 1 || state ==2)
{
state = 0;
this.anim.SetInteger("State", state);
}
}
}
}