Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CrazyManCM · Jun 10, 2019 at 08:41 PM · gameobjectshootingtransform.positionvector2

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;
             
         }
         
         
 
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
 
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CrazyManCM · Jun 11, 2019 at 07:31 AM 0
Share

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;
              
          }
          
          
  
      }

avatar image BradyIrv CrazyManCM · Jun 11, 2019 at 09:54 AM 0
Share

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.

avatar image CrazyManCM BradyIrv · Jun 12, 2019 at 12:56 PM 0
Share

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:

  1. Visibility down to 30%

  2. Disable the box collider

  3. Pause animation

  4. Teleport

  5. Wait 2 seconds

  6. Visibility back up to 100%

  7. Enable the box collider

  8. Unpause animation

Thanks for your help

Show more comments
avatar image CrazyManCM · Jun 17, 2019 at 06:29 PM 0
Share

Thanks this helped a lot.

avatar image
0

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;
              
          }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges