Howdy again,
I've been trying somethings to get my enemy to slide, that was answered [here][1]. but now, I've got a problem with getting it to slide and fall at the same time. I'm trying to get the green dot to fall to the bottom of the screen.
It falls to a certain position and then stops, both falling and sliding. A screenshot of where it stops.
![Screenshot][2]
[Screenshot 1][3]
My script looks like this.
using UnityEngine;
using System.Collections;
using Legacy;
public class EnemyAI : MonoBehaviour {
public Transform destroyEffect;
public Transform bottomSpike;
public float moveSpeed = 0.5f;
private Transform self;
private Vector3 newPos;
private Spawner spawner;
private bool isDestroyed = false;
void Awake() {
self = transform;
newPos = new Vector3(-2, transform.position.y);
spawner = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent();
}
// Update is called once per frame
void Update() {
SlideEnemy();
}
void SlideEnemy() {
Vector3 posA = new Vector3(-2, transform.position.y);
Vector3 posB = new Vector3(2, transform.position.y);
if (transform.position == posA) {
newPos = posB;
}
else if (transform.position == posB) {
newPos = posA;
}
if (!isDestroyed) {
transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * moveSpeed);
transform.position = Vector3.MoveTowards(transform.position, bottomSpike.position , Time.deltaTime * moveSpeed);
}
}
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "Player") {
//TO-DO: Destroy enemy and reset player position
StartCoroutine(DestroyEnemy());
isDestroyed = true;
GameManager.instance.IncreaseScore();
spawner.SpawnEnemy();
}
}
IEnumerator DestroyEnemy() {
Transform t = Instantiate(destroyEffect) as Transform;
t.parent = this.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.gameObject.SetActive(true);
this.GetComponent().material.color = new Color(0, 0, 0, 0);
yield return new WaitForSeconds(5);
t.gameObject.SetActive(false);
this.gameObject.SetActive(false);
Destroy(t.gameObject);
}
}
Thanks in advance.
[1]: http://answers.unity3d.com/questions/951030/enemy-lerping-between-two-vectors.html
[2]: http://s27.postimg.org/v3de3x167/Dot_Dot_Boom.png
[3]: http://postimg.org/image/ts4ps5x3l/
↧