- Home /
How do I move a gameobject to the postion of the player?
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;
}
}
}
Answer by BradyIrv · Jun 10, 2019 at 09:26 PM
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;
}
}
Thanks, I appreciate the help. With this Part of the code, I was trying to make the enemy less visible and remove the hitbox for 2 seconds.
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;
}
}
Ah okay, just make sure that you only call it once then and not every frame. It will work now if you add it to a death function and call it when the enemy dies.
Hey so I tested your code and it doesn't work. I don't know why but I would appreciate if you help me again. So I just want the gameobject (Enemy) to first change the visibility to 30%, then disable the box collider that you can walk through the gameobject (Enemy), then disable the animation of the gameobject (Enemy), the gameobject (Enemy) doesn't move its just placed in one spot and animated, and last teleport the gamobject (Enemy) to the postion of the player when he shot the gameobject (Enemy). I only want all of this to occur if the gameobject (Enemy) reaches 0 health. After 2 seconds I want to reverse the whole process besides teleporting. So:
Visibility down to 30%
Disable the box collider
Pause animation
Teleport
Wait 2 seconds
Visibility back up to 100%
Enable the box collider
Unpause animation
Thanks for your help
Answer by AtomicDev · Jun 10, 2019 at 08:47 PM
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;
}
Your answer
Follow this Question
Related Questions
How would you check if an object is closer to the bottom side or top side of another object 1 Answer
Spawn objects at bottom of screen / camera 2 Answers
How can i change the sprite of my gameobject when it's located within a specific range 1 Answer
Input Axis LeftAnalogHorizontal is not setup. 3 Answers
How do you make bullets face the direction it's going? 0 Answers