- Home /
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.
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.
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..
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.
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);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612203205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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