Search Unity

How do I move towards a target in 2D?

Discussion in '2D' started by meikopfaffmann03, Nov 14, 2019.

  1. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    This script should move the Sprite towards 0,0 but it does not work. It gives me the error at line 16, letter 18: " Cannot implicitly convert type 'UnityEngine.Vector2' to 'UnityEngine.Transform' "
    How can I write this script to avoid the error? Thanks in advance.

    The Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile_1 : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     public float speed = 1f;
    9.     public Transform target;
    10.     private Vector2 target2;
    11.     private Vector2 position;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         target = new Vector2(0.0f, 0.0f);
    17.         position = gameObject.transform.position;
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D col)
    21.     {
    22.         if (col.gameObject.tag == "Player")
    23.         {
    24.             Destroy(gameObject);
    25.         }
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         Vector2 direction = target.position - transform.position;
    31.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    32.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    33.      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
    34.         float step = speed * Time.deltaTime;
    35.         transform.position = Vector2.MoveTowards(transform.position, target2, step);
    36.     }
    37. }
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Hello,
    If we break down the error message, we can see that something goes wrong on line 16.
    target = new Vector2(0.0f, 0.0f);

    Here, target is of type Transform, and you are then trying to set it as a Vector2, hence the error.

    What you probably want to do, is to instead set targets position (which is of type Vector3), with the vector you are defining.
    target.position = new Vector2(0.0f, 0.0f);
     
    MisterSkitz likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    As a note, never ever change the Transform when using Physics. When you add a Rigidbody2D you're asking for it to be in charge of the Transform itself which should make sense as the primary purpose of the simulation is to set body poses then write that to the Transform. By setting the Transform you're doing it backwards and undermines the physics system.

    Always go through the Rigidbody2D using calls like Rigidbody2D.MovePosition and Rigidbody2D.MoveRotation. Setting the Transform position directly causes the body to be teleported to the new position and causes collision overlaps and tunnelling and isn't compatible with interpolation (smooth per-frame movement). Performance-wise it's also slower.
     
    Last edited: Nov 18, 2019
    Ted_Wikman and MisterSkitz like this.
  4. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    Thank you all!