59 lines
1.3 KiB
C#

using System;
using UnityEngine;
/// <summary>
/// Playermovement.
/// </summary>
public class PlayerController : MonoBehaviour
{
/*[SerializeField] public float speed;
[SerializeField] public float acceleration;
*/
[SerializeField] public float jumpPower;
private Rigidbody2D body;
private bool isGround = true;
private Animator jumpAnim;
void Start()
{
body = this.GetComponent<Rigidbody2D>();
jumpAnim = this.GetComponent<Animator>();
}
private void Update()
{
/*
// run logic auto
speed += acceleration * Time.deltaTime;
transform.Translate(new Vector2(1f, 0f) * speed * Time.deltaTime);
*/
// jump logic + jump animation
if (Input.GetKey(KeyCode.W) && isGround)
{
Jump();
jumpAnim.SetBool("jump", true);
isGround = false;
}
}
// jump method logic + jump animation
void Jump()
{
Vector2 velocity = body.velocity;
velocity.y = jumpPower;
body.velocity = velocity;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.tag == "Ground")
{
isGround = true;
jumpAnim.SetBool("jump", false);
}
}
}