Reputation: 81
The following script is supposed to make the enemy sprite follow the player, but all it does is teleport left and right of the player.
The speed I set is connected to the distance the enemy teleports. If you need anything else besides this script, just tell me I'm quite new to Unity and C#.
The error I'm getting is:
Assertion failed on expression: 'task.rasterData.vertexBuffer == NULL'
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
And my code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
public Rigidbody2D target;
public float speed = 10f;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector2 dir = target.position - rb.position;
rb.MovePosition(dir.normalized * speed);
}
}
Upvotes: 0
Views: 334
Reputation: 4551
As BugFinder says, yo need the transform. Also although you are working in 2d, a transform's position is a vector3. Find some modifications as suggestions for your code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
public Transform target;
public float speed = 10f;
public Transform rb;
void Start()
{
//You can get these programatically or attach them ni the editor. Seems that
//target is the player and the rb is the follower. You can make the variables
//public attach them in the editor so that they can be accessed in the code
// rb = GetComponent<Rigidbody2D>();
// target =
//GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
}
void Update()
{
Vector3 dir = target.position - rb.position;
rb.position = rb.position + (dir.normalized * speed);
}
}
Hope that helps
Upvotes: 0