Untitled
unknown
plain_text
2 years ago
1.5 kB
5
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
[SerializeField] private float speed = 2f;
[SerializeField] private float jumpForce = 500f;
private Rigidbody2D rb;
private Animator animator;
private bool grounded;
private bool started;
private bool jumping;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
grounded = true;
}
private void Update()
{
if (Input.GetKeyDown("space"))
{
if (started && grounded)
{
animator.SetTrigger("Jump");
grounded = false;
jumping = true;
}
else
{
animator.SetBool("GameStarted", true);
started = true;
}
}
animator.SetBool("Grounded", grounded);
}
private void FixedUpdate()
{
if (started)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
if (jumping)
{
rb.AddForce(new Vector2(0f, jumpForce));
jumping = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
grounded = true;
}
}
}
Editor is loading...
Leave a Comment