Методы из скрипта стрелка:
Синтаксис:
Используется csharp
//Нанесение урона
void StartDamage () {
moveSpeedDirection = Mathf.Sign (transform.position.x - player.transform.position.x);
moveSpeed = Mathf.Lerp (0f, 0.1f, Time.deltaTime);
StartCoroutine ("Attacking");
}
//Нанесение урона
IEnumerator Attacking () {
animator.SetBool ("attack", true);
yield return new WaitForSeconds (0.3f);
animator.SetBool ("attack", false);
yield return new WaitForSeconds (0.5f);
CreateArrow ();
Debug.Log ("Выстрел");
}
public void CreateArrow() {
Vector3 velo = new Vector3 (tran.position.x, tran.position.y + 0.8f, tran.position.z);
Instantiate (arrow, velo, Quaternion.identity);
}
void StartDamage () {
moveSpeedDirection = Mathf.Sign (transform.position.x - player.transform.position.x);
moveSpeed = Mathf.Lerp (0f, 0.1f, Time.deltaTime);
StartCoroutine ("Attacking");
}
//Нанесение урона
IEnumerator Attacking () {
animator.SetBool ("attack", true);
yield return new WaitForSeconds (0.3f);
animator.SetBool ("attack", false);
yield return new WaitForSeconds (0.5f);
CreateArrow ();
Debug.Log ("Выстрел");
}
public void CreateArrow() {
Vector3 velo = new Vector3 (tran.position.x, tran.position.y + 0.8f, tran.position.z);
Instantiate (arrow, velo, Quaternion.identity);
}
Скрипт стрелы:
Синтаксис:
Используется csharp
[RequireComponent(typeof (Controller2D))]
public class Arrow: MonoBehaviour {
Vector3 velocity;
float direction;
public float moveSpeed;
Controller2D controller;
bool Check;
GameObject archer;
private bool isFacingRight = true;
void Awake () {
StartCoroutine ("Pause");
archer = GameObject.Find ("Archer");
direction = archer.GetComponent<Archer> ().moveSpeedDirection;
velocity.x = Vector2.right.x * moveSpeed * direction;
}
void Update() {
if (velocity.x > 0 && !isFacingRight)
//отражаем персонажа вправо
Flip ();
//обратная ситуация. отражаем персонажа влево
else if (velocity.x < 0 && isFacingRight)
Flip ();
transform.Translate (velocity);
}
private void Flip()
{
//меняем направление движения персонажа
isFacingRight = !isFacingRight;
//получаем размеры персонажа
Vector3 theScale = transform.localScale;
//зеркально отражаем персонажа по оси Х
theScale.x *= -1;
//задаем новый размер персонажа, равный старому, но зеркально отраженный
transform.localScale = theScale;
}
IEnumerator Pause() {
yield return new WaitForSeconds (5f);
Destroy (gameObject);
}
}
public class Arrow: MonoBehaviour {
Vector3 velocity;
float direction;
public float moveSpeed;
Controller2D controller;
bool Check;
GameObject archer;
private bool isFacingRight = true;
void Awake () {
StartCoroutine ("Pause");
archer = GameObject.Find ("Archer");
direction = archer.GetComponent<Archer> ().moveSpeedDirection;
velocity.x = Vector2.right.x * moveSpeed * direction;
}
void Update() {
if (velocity.x > 0 && !isFacingRight)
//отражаем персонажа вправо
Flip ();
//обратная ситуация. отражаем персонажа влево
else if (velocity.x < 0 && isFacingRight)
Flip ();
transform.Translate (velocity);
}
private void Flip()
{
//меняем направление движения персонажа
isFacingRight = !isFacingRight;
//получаем размеры персонажа
Vector3 theScale = transform.localScale;
//зеркально отражаем персонажа по оси Х
theScale.x *= -1;
//задаем новый размер персонажа, равный старому, но зеркально отраженный
transform.localScale = theScale;
}
IEnumerator Pause() {
yield return new WaitForSeconds (5f);
Destroy (gameObject);
}
}
Если на сцене всего один враг-стрелок, то данная схема работает, а если много врагов и все они патрулируют в разных направлениях, то конечно это не катит. Как сделать правильно?