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 Lineweaver · Jun 14, 2019 at 10:57 AM · collisioninstantiate2d gamepositionmovetowards

Unity 2D - MoveTowards position not updating, not recgonizing new position

I'm trying to create a little game where when you press a key button, an object is created above a target's head, and then starts moving downwards toward it, dealing damage upon collision. This is the object creation script, attached to a button:

 public class Skill : MonoBehaviour {

 public Transform enemy;
 public GameObject skull;

 private Button _button;
 public KeyCode key;

 void Awake()
 {
     _button = GetComponent<Button>();
 }

 private void Update()
 {
     if (Input.GetKeyDown(key))
     {
         _button.onClick.Invoke();
     }
 }

 public void ActivateSkill()
 {
     Instantiate(skull, new Vector2(enemy.transform.position.x, enemy.transform.position.y + 8), Quaternion.identity);
 }

}

This is the object movement script, attached to the spawning object itself:

 public class SkillBehavior : MonoBehaviour {

 public Transform enemy;
 public GameObject bloodEffect;
 public float speed;

 void Update ()
 {
     transform.position = Vector2.MoveTowards(transform.position, enemy.position, speed * Time.deltaTime);
 }

 void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.name == "Enemy")
     {
         Enemy.enemyHealth -= 0.5f;
         Debug.Log("Skill activated, enemy health is " + Enemy.enemyHealth);
         Instantiate(bloodEffect, new Vector2(enemy.transform.position.x, enemy.transform.position.y), Quaternion.identity);
     }
 }

}

The problem I'm having is that the position that the object doesn't recgonize when the target has a new position, and that the OnCollisionEnter2D function doesn't work. For example, if the target moved to the right, the object will instantiate above it, but will move towards the target's old position. Any help would be much appreciated ^^ P.S both target and object have box colliders 2D.

Comment
Add comment · Show 6
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 Hellium · Jun 14, 2019 at 11:40 AM 0
Share

Where do you set the reference to the enemy?

If you rely on the Physics engine to detect collision, then, you should not use transform.Translate, but the appropriate Rigidbody's method.

avatar image Lineweaver Hellium · Jun 14, 2019 at 12:06 PM 0
Share

Forgot to mention it, the enemy is public so I just drag the target into it on the inspector, and it has a rigidbody2d. I'm not sure how to use the transform.Translate here though..

avatar image Bincredible Lineweaver · Jun 14, 2019 at 12:14 PM 0
Share

If you are dragging the enemy reference in before the instantiation, then doesn't that mean you have reference to the prefab, and not the game object? This may be the reasoning behind the position update not being recognized - you aren't actually referencing the object in the scene.

Show more comments

1 Reply

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

Answer by Hellium · Jun 14, 2019 at 12:43 PM

 public class Skill : MonoBehaviour
 {
     public Transform enemy;
     public SkillBehavior skull;
     private Button _button;
     public KeyCode key;
 
     void Awake()
     {
         _button = GetComponent<Button>();
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(key))
         {
             _button.onClick.Invoke();
         }
     }
     public void ActivateSkill()
     {
         SkillBehavior instantiatedSkill = Instantiate(skull, new Vector2(enemy.transform.position.x, enemy.transform.position.y + 8), Quaternion.identity);
         instantiatedSkill.enemy = enemy;
     }
 }




 public class SkillBehavior : MonoBehaviour
 {
     public Transform enemy;
     public GameObject bloodEffect;
     public float speed;
     private Rigidbody2D _rigidbody;
 
     void Awake()
     {
         _rigidbody = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate ()
     {
         float maxDistance = speed * Time.deltaTime;
         Vector2 deltaPosition = Vector2.ClampMagnitude( ((Vector2) enemy.position) - _rigidbody.position, maxDistance );
         _rigidbody.MovePosition( _rigidbody.position + deltaPosition );
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.name == "Enemy")
         {
             // Remove the `static` keyword before `enemyHealth` in the `Enemy` class
             Enemy enemy = collision.gameObject.GetComponent<Enemy>();
             if( enemy != null )
             {
                 enemy.enemyHealth -= 0.5f;
                 Debug.Log("Skill activated, enemy health is " + enemy.enemyHealth);
                 Instantiate(bloodEffect, new Vector2(enemy.transform.position.x, enemy.transform.position.y), Quaternion.identity);
             }
         }
     }
 }
Comment
Add comment · Show 1 · 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 Lineweaver · Jun 14, 2019 at 02:12 PM 0
Share

It works perfectly now, thank you for your help ^^

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

216 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 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

Collider not triggered when change posision 1 Answer

How do I get the global position 2 Answers

Unity3D - Playback object array of position (with dynamic velocity) 0 Answers

how can I instantiate an object specifically at the intersection of a collider and a mesh 1 Answer

Text never displays count variable 1 Answer


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