You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonMovement : MonoBehaviour
{
[SerializeField] KeyCode FlapKey;
Rigidbody2D _rigidbody;
float _flapTime = 0f;
[SerializeField] float FlapForce = 300f;
[SerializeField] float FlapLength = .25f;
bool _inCloud;
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("ontriggerenter");
_inCloud = true;
}
private void OnTriggerExit2D(Collider2D collision)
{
_inCloud = false;
}
void Start()
{
_rigidbody = this.GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKeyDown(FlapKey))
{
_flapTime = FlapLength;
}
}
void FixedUpdate()
{
if (_flapTime > 0f)
{
_rigidbody.AddForce(new Vector2(0f, FlapForce));
}
_flapTime -= Time.deltaTime;
float horizontalSpeed = .4f;
if (_inCloud)
{
horizontalSpeed = -1f;
}
transform.position = new Vector3(transform.position.x + horizontalSpeed, transform.position.y, transform.position.z);
}
}