Doesn't work MoveTowards and Lerp
Hello guys! Had a problem: got 2 classes: EnemyCreate:
using UnityEngine;
using System.Collections;
public class EnemyCreate : MonoBehaviour {
public GameObject enemyObject;
void Start () {
StartCoroutine(CreatingEnemy());
}
void Update (){
}
IEnumerator CreatingEnemy()
{
while(true)
{
CreateEnemy();
yield return new WaitForSeconds(1);
}
}
void CreateEnemy()
{
GameObject enemy = (GameObject)Instantiate(enemyObject);
enemy.transform.parent = this.gameObject.transform;
}
}
}
and Enemy:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
Transform[] wayPoint;
float enemySpeed = 5.0f;
void Start () {
GetWay();
}
void GetWay()
{
int waysCount = GameObject.FindGameObjectWithTag ("Ways").transform.childCount;
int wayNumber = Random.Range (1, waysCount+1);
int wayPoints = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild
("Way" + wayNumber).childCount;
// array of enemy way points
wayPoint = new Transform[wayPoints];
for (int i = 0; i < wayPoints; i++) {
wayPoint [i] = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild
("Way" + wayNumber).transform.FindChild ("Point" + (i + 1));
}
}
void Update()
{
transform.position = Vector3.MoveTowards (transform.position, wayPoint [0].position, enemySpeed * Time.deltaTime);
}
}
This doesn't work: transform.position = Vector3.MoveTowards (transform.position, wayPoint [0].position, enemySpeed Time.deltaTime);*
The code creates enemies with different ways to move, wayPoint[0] exist, all enemies has the same wayPoint[0] and start point(point where enemy instantiate). ATM I want to make enemy move just from start point to wayPoint[0].
Can someone help me?
Answer by LucianoMacaDonati · Oct 30, 2016 at 07:40 PM
Hello ! First of all, I noticed your EnemyCreate.cs will never stop creating enemies. Intended or not, this may make your debugging harder. I recommend creating only 1, testing it, and multiplying the results when they work.
I also see you have a lot going on in your "GetWay" function. I assume you have an Empty game object named "Ways" and you want your enemies to traverse those waypoints randomly, correct?
I would start by getting a reference to those waypoints on Start(), instead of looking for them every time you want a new waypoint. I would do this by tagging my waypoints, and then using your current MoveTowards code to move towards the next waypoint. When we get too close (you can change this to a bigger number like 0.01f) then we get another random waypoint from our references.
GameObject[] waypointsRef;
Vector3 destination;
void Start()
{
waypointsRef = GameObject.FindGameObjectsWithTag("Waypoints");
GetWaypoint();
}
Vector3 GetWaypoint()
{
destination = waypointsRef[Random.Range(0, waypointsRef.Length - 1)].trasnform.position;
}
void Update()
{
Vector3 source = transform.position;
if ((destination - source).magnitude < Mathf.Epsilon)
transform.position = Vector3.MoveTowards(source,destination, enemySpeed * Time.deltaTime);
else
GetWaypoint();
}
Let me know how it goes.
@maca Thanks for your answer, I used GetWay() because I use 3 possible ways for enemy to go. It's look like this: Its work properly.
The problem I had is that enemy doesn't move at all. I've add some logs to my code(class Enemy):
void Start () {
GetWay();
Debug.Log ("Enemy actual position: " + transform.position);
Debug.Log ("Position to go to: " + wayPoint [0].position);
}
void GetWay()
{
int waysCount = GameObject.FindGameObjectWithTag ("Ways").transform.childCount;
int wayNumber = Random.Range (1, waysCount+1);
int wayPoints = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild
("Way" + wayNumber).childCount;
// array of enemy way points
wayPoint = new Transform[wayPoints];
for (int i = 0; i < wayPoints; i++) {
wayPoint [i] = GameObject.FindGameObjectWithTag ("Ways").transform.FindChild
("Way" + wayNumber).transform.FindChild ("Point" + (i + 1));
}
}
void Update()
{
Vector3 actualPosition = transform.position;
transform.position = Vector3.$$anonymous$$oveTowards (actualPosition, wayPoint [0].position, enemySpeed); // * Time.deltaTime);
}
And this logs show me this:
As you see, there is coordinates of enemy and coordinates to go to, but $$anonymous$$oveTowards and Lerp doesn't work. I don't understand what the problem can be here.
Now I see the problem: $$anonymous$$oveTowards will return the destination position if the difference between the start and destination ((destination - source).magnitude < maxDistanceDelta) is less than the step (more info here). In this case, there is only .5f on the z axis and that's not far away enough to move (in other words, you're already there).
In my example, I showed you how to get the next waypoint when you reached the first one. Your problem is that the enemy is already at the waypoint, and you're not updating that waypoint for your enemy to move to something that is further away.
On a separate note, I advice you to do something different (look the example I gave you) to get the waypoints list. I foresee that what you're currently doing will be a problem further on your development.
We don't understand each other, but I find a problem. The problem is that all time when I run the program, float enemySpeed from Enemy class equals "0". Thats why 3rd variable (enemySpeed*Time.deltaTime) in Vector3.$$anonymous$$oveTowards always be "0". Now I just make enemySpeed as public variable, and change the value of it in prefab Enemy in unity, and its work. I don't know exactly why this happens. Thanks @maca one more time for your answers!