I am making a game when you gather but my MoveTowards method just teleports the object there or to the position I asked. I have made my own MoveTo method so here are my 3 scripts. One is an IUNit script which is an interface and holds the method MoveTo:
using System;
using UnityEngine;
public interface IUnit
{
void MoveTo(Vector2 position, float stopDistance, Action onArrivedAtPosition);
}
Here is my Unit script which is where the AI goes:
using UnityEngine;
public class Unit: MonoBehaviour, IUnit
{
public float speed;
public void MoveTo(Vector2 position, float stopDistance, System.Action arrivedAtPosition)
{
if (Vector2.Distance(transform.position, position) >= stopDistance)
{
transform.position = Vector2.MoveTowards(transform.position, position, speed * Time.deltaTime);
}
}
}
Then my GathererAI which activates my script:
using UnityEngine;
[RequireComponent(typeof(Unit))]
public class GathererAI : MonoBehaviour
{
public Transform treeNode;
public Transform storageNode;
public IUnit unit;
private void Start()
{
unit = gameObject.GetComponent<IUnit>();
unit.MoveTo(treeNode.position, 10f, null);
}
}
But the thing is it just teleports to the position without slowly moving. Please help? Also, how do you start an action?
,I am making a game when you gather but my MoveTowards method just teleports the object there or to the position I asked. I have made my own MoveTo method so here are my 3 scripts. One is an IUNit script which is an interface and holds the method MoveTo:
using System;
using UnityEngine;
public interface IUnit
{
void MoveTo(Vector2 position, float stopDistance, Action onArrivedAtPosition);
}
Here is my Unit script which is where the AI goes:
using UnityEngine;
public class Unit: MonoBehaviour, IUnit
{
public float speed;
public void MoveTo(Vector2 position, float stopDistance, System.Action arrivedAtPosition)
{
if (Vector2.Distance(transform.position, position) >= stopDistance)
{
transform.position = Vector2.MoveTowards(transform.position, position, speed * Time.deltaTime);
}
}
}
Then my GathererAI which activates my script:
using UnityEngine;
[RequireComponent(typeof(Unit))]
public class GathererAI : MonoBehaviour
{
public Transform treeNode;
public Transform storageNode;
public IUnit unit;
private void Start()
{
unit = gameObject.GetComponent<IUnit>();
unit.MoveTo(treeNode.position, 10f, null);
}
}
But the thing is it just teleports to the position without slowly moving. Please help? Also, how do you start an action?