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