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.
71 lines
1.6 KiB
71 lines
1.6 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;
|
|
Animator _animator;
|
|
bool _inCloud;
|
|
[SerializeField] GameObject _fireballPrefab;
|
|
[SerializeField] GameObject _fireballSpawnPoint;
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
{
|
|
Debug.Log("ontriggerenter");
|
|
_inCloud = true;
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D collision)
|
|
{
|
|
_inCloud = false;
|
|
}
|
|
void Start()
|
|
{
|
|
_rigidbody = this.GetComponent<Rigidbody2D>();
|
|
_animator = this.GetComponent<Animator>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(FlapKey))
|
|
{
|
|
Flap();
|
|
Shoot();
|
|
}
|
|
}
|
|
|
|
void Flap()
|
|
{
|
|
_flapTime = FlapLength;
|
|
_animator.Play("fly");
|
|
}
|
|
|
|
void Shoot()
|
|
{
|
|
GameObject.Instantiate(_fireballPrefab, _fireballSpawnPoint.transform.position, Quaternion.identity);
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
}
|
|
|