Flèches droites peuvent changer d'angle #7
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -30,9 +31,12 @@ public class Projectile : MonoBehaviour
|
|||||||
private float _initialX;
|
private float _initialX;
|
||||||
private float _initialY;
|
private float _initialY;
|
||||||
private float _destinationX;
|
private float _destinationX;
|
||||||
|
private float _destinationY;
|
||||||
|
private float _enemyPosY = 0f;
|
||||||
private float _speedTime = 0f;
|
private float _speedTime = 0f;
|
||||||
|
|
||||||
private float _initialXDistance;
|
private float _initialXDistance;
|
||||||
|
private float _initialYDistance;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
@ -40,23 +44,28 @@ public class Projectile : MonoBehaviour
|
|||||||
_initialX = transform.position.x;
|
_initialX = transform.position.x;
|
||||||
_initialY = transform.position.y;
|
_initialY = transform.position.y;
|
||||||
_destinationX = _vectorEnd.x - _initialX - _enemySpeed;
|
_destinationX = _vectorEnd.x - _initialX - _enemySpeed;
|
||||||
|
_destinationY = _vectorEnd.y - _initialY;
|
||||||
|
|
||||||
_detectionLinked.gameObject.GetComponent<Detection>().EntityLinked = _target;
|
_detectionLinked.gameObject.GetComponent<Detection>().EntityLinked = _target;
|
||||||
_detectionLinked.gameObject.GetComponent<Detection>().ProjectileDamage = _damage;
|
_detectionLinked.gameObject.GetComponent<Detection>().ProjectileDamage = _damage;
|
||||||
|
|
||||||
_initialXDistance = Mathf.Abs(_initialX - _destinationX);
|
_initialXDistance = Mathf.Abs(_initialX - _destinationX);
|
||||||
|
_initialYDistance = Mathf.Abs(_initialY - _destinationY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (straightProjectile)
|
if (straightProjectile)
|
||||||
{
|
{
|
||||||
|
|
||||||
transform.position = Vector2.Lerp(new Vector2(_initialX, _initialY),
|
transform.position = Vector2.Lerp(new Vector2(_initialX, _initialY),
|
||||||
new Vector2(_vectorEnd.x, _vectorEnd.y), _time/_duration);
|
new Vector2(_vectorEnd.x, _vectorEnd.y), _time/_duration);
|
||||||
|
|
||||||
|
DetStraigthArrowAngle();
|
||||||
|
|
||||||
//TODO: Implementer cette methode pour mieux faire fonctionner le projectile lorsque possible.
|
//TODO: Implementer cette methode pour mieux faire fonctionner le projectile lorsque possible.
|
||||||
//transform.LookAt(VectorEnd, Vector3.forward);
|
//transform.LookAt(VectorEnd, Vector3.forward);
|
||||||
|
|
||||||
_time += Time.deltaTime;
|
_time += Time.deltaTime;
|
||||||
|
|
||||||
if (transform.position.x >= _vectorEnd.x)
|
if (transform.position.x >= _vectorEnd.x)
|
||||||
@ -67,7 +76,7 @@ public class Projectile : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
float x = _speedTime;
|
float x = _speedTime;
|
||||||
float y = (_angle*-Mathf.Pow(x, 2) + _destinationX * x);
|
float y = (_angle*-Mathf.Pow(x, 2) + _destinationX * x);
|
||||||
|
|
||||||
transform.position = new Vector2(_initialX + x*_angle, _initialY + y);
|
transform.position = new Vector2(_initialX + x*_angle, _initialY + y);
|
||||||
|
|
||||||
@ -83,6 +92,45 @@ public class Projectile : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DetStraigthArrowAngle()
|
||||||
|
{
|
||||||
|
|
||||||
|
if(_target != null) _enemyPosY = _target.Position.y;
|
||||||
|
|
||||||
|
float angleX = (_destinationX) / _initialXDistance;
|
||||||
|
float angleY = (_destinationY) / _initialYDistance;
|
||||||
|
|
||||||
|
//Obtenir la difference entre la position Y du chateau et de l'ennemie.
|
||||||
|
float diffY = transform.position.y - _enemyPosY;
|
||||||
|
|
||||||
|
//D'abord,on regarde si l'ennemi et l'unite sont a la meme hauteur.
|
||||||
|
//Si ce n'est pas le cas, on modifie l'angle de la fleche. Sinon, on ne change rien.
|
||||||
|
if (diffY >= 0.1 || diffY <= -0.1)
|
||||||
|
{
|
||||||
|
|
||||||
|
float lerpStepS;
|
||||||
|
|
||||||
|
//Ensuite, on regarde la position entre l'ennemi et l'unite.
|
||||||
|
//Si cette distance est plus petite qu'un certain nombre, on utilise un calcul differents
|
||||||
|
//pour determine l'angle que la fleche doit prendre.
|
||||||
|
if (angleX > 0.23) lerpStepS = Mathf.Rad2Deg * Mathf.Atan(angleX / angleY);
|
||||||
|
|
||||||
|
//Si la position est negative, on utilise un calcul different pour determiner l'angle.
|
||||||
|
//(Il faudra tester si ca marche lorsque le chateau pourrait tirer en arriere).
|
||||||
|
else if (angleX >= 0) lerpStepS = Mathf.Rad2Deg * Mathf.Atan(angleY / angleX);
|
||||||
|
|
||||||
|
//Calcul utilise pour tenter de mieux faire fonctionner l'angle de la fleche lorsque l'ennemi est
|
||||||
|
//proche de l'unite.
|
||||||
|
else {
|
||||||
|
|
||||||
|
lerpStepS = -Mathf.Rad2Deg * Mathf.Atan(angleY / angleX);
|
||||||
|
}
|
||||||
|
|
||||||
|
float angleS = Mathf.Lerp(lerpStepS, 0, 0);
|
||||||
|
transform.eulerAngles = new Vector3(0, 0, angleS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Getter and Setter
|
//Getter and Setter
|
||||||
public float EnemySpeed
|
public float EnemySpeed
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user