So I want to move the enemy or teleport the enemy to the position of the player when he is shot but I don’t seem to be able to do that. I would appreciate the help thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour {
public float alphalevel = .3f;
public float alphaleve = 1f;
public int health = 40;
public GameObject deathEffect;
public void TakeDamage (int damage)
{
health -= damage;
if (health <= 0)
{
Teleport();
}
}
void Teleport()
{
StartCoroutine(Hello());
GetComponent<SpriteRenderer>().color = new Color (1,1,1,alphalevel);
GetComponent<BoxCollider2D> ().enabled = false;
GetComponent<Animator> (). enabled = false;
IEnumerator Hello()
{
yield return new WaitForSeconds(2f);
GetComponent<SpriteRenderer>().color = new Color (1,1,1,alphaleve);
GetComponent<BoxCollider2D> ().enabled = true;
GetComponent<Animator> (). enabled = true;
}
}
}
First you need a reference Transform to the Player. Then you move it, just like this:
(P.S: There may be a better solution to what I am presenting, this is just a simple solution)
public Transform playerObject;
void Teleport()
{
StartCoroutine(Hello());
GetComponent<SpriteRenderer>().color = new Color (1,1,1,alphalevel);
GetComponent<BoxCollider2D> ().enabled = false;
GetComponent<Animator> (). enabled = false;
transform.position = playerObject.position;
IEnumerator Hello()
{
yield return new WaitForSeconds(2f);
GetComponent<SpriteRenderer>().color = new Color (1,1,1,alphaleve);
GetComponent<BoxCollider2D> ().enabled = true;
GetComponent<Animator> (). enabled = true;
}
I’m not sure what you were trying to achieve with the Hello Coroutine, but when the enemy dies it calls itself recursively every frame which is something you really need to be careful about. I’ve written a function that will let you achieve your goal of moving the enemy to the player, if you increase the speed of the Lerp, it will be a teleport. If you keep the speed low then it will just move the enemy to the player. I also added some testing variables and improved the script to what I think you were going for. I hope it is what you were asking for or you can manipulate it how you want:
public class Enemy : MonoBehaviour {
public float alphalevel = .3f;
public float alphaleve = 1f;
public int health = 40;
public GameObject deathEffect;
// Move or Teleport to player
public Transform player; // Reference to player position
[Range(0, 2f)] public float speed; // Move speed (0.01f slow, 2 teleport)
public float stoppingDistance; // When to stop moving towards the player
public bool dmg; // TESTING
private bool canMove; // Stopping distance
private bool isDead; // Ensure death function runs once
private void Update()
{
//testing damage on Enemy (so I could see if it worked or not)
if (dmg)
{
TakeDamage(10);
dmg = false;
}
// When both true, move to player
if (canMove) Teleport();
}
public void TakeDamage(int damage)
{
// Start moving to player or teleport when damage is received
if (canMove == false) canMove = true;
health -= damage;
if (health <= 0 && !isDead)
{
isDead = true;
// Die, istantiate death effect
}
}
void Teleport()
{
// Check distance to player, if already within stopping distance don't move
if(Vector3.Distance(this.transform.position, player.position) < stoppingDistance)
{
canMove = false; // Stop enemy from moving
return; // exit function without moving
}
// Moves Enemy from current position toward player position
this.transform.position = Vector3.Lerp(this.transform.position, player.position, speed);
}
}
Is this what you are looking to do? It requires some setup. Your animator needs to have two animations, one for alive and the other for dead. Then add a parameter to the animator, a boolean, named “isDead” and make a transition between the alive and dead states with the boolean as the transition condition.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeleportToPlayerEnemy : MonoBehaviour
{
public float lowAlpha = .3f;
public float highAlpha = 1f;
public int health = 40;
public GameObject deathEffect;
public SpriteRenderer sRenderer;
public BoxCollider2D bCollider;
public Animator anim;
public Transform player; // Reference to player position
private bool isDead; // Ensure death function runs once
public bool dmg; // TESTING
private void Update()
{
//testing damage on Enemy (so I could see if it worked or not)
if (dmg)
{
TakeDamage(10);
dmg = false;
}
}
public void TakeDamage(int damage)
{
if(!isDead)
{
health -= damage;
if (health <= 0)
{
Debug.Log("I Died");
isDead = true;
DoStuff();
}
}
}
void DoStuff()
{
// 1. Visibility down to 30 %
sRenderer.color = new Color(sRenderer.color.r, sRenderer.color.g, sRenderer.color.b, lowAlpha);
// 2. Disable the box collider
bCollider.enabled = false;
// 3. Pause animation
anim.SetBool("isDead", true);
// 4. Teleport
this.transform.position = player.position;
// 5. Wait 2 seconds before reset
StartCoroutine(ResetValues(2f));
}
IEnumerator ResetValues(float waitTime)
{
// 5 Continued. Wait x Time
yield return new WaitForSeconds(waitTime);
Debug.Log("Reset");
// 6. Visibility normal
sRenderer.color = new Color(sRenderer.color.r, sRenderer.color.g, sRenderer.color.b, highAlpha);
// 7. Enable the box collider
bCollider.enabled = true;
// 8. Unpause animation
anim.SetBool("isDead", false);
// ONLY IF FULLY RESETTING THE ENEMY SO ALL THIS CAN RUN AGAIN
// 9. Reset health
health = 40;
// 10. Reset dead boolean
isDead = false;
}
}