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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile_1 : MonoBehaviour
{
public Rigidbody2D rb;
public float speed = 1f;
public Transform target;
private Vector2 target2;
private Vector2 position;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
target = new Vector2(0.0f, 0.0f);
position = gameObject.transform.position;
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
private void Update()
{
Vector2 direction = target.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target2, step);
}
}