Thanks to some(mostly) help from you guys I ended up with this script that moves an object from its spawn position to a different/designated position.
using UnityEngine;
using System.Collections;
public class WaypointBasic : MonoBehaviour
{
public GameObject waypointInvader;
GameObject go;
Vector3 movePosition = new Vector3(75f, 1.7f, 75f);
public float speed = 5f;
void Start()
{
go = Instantiate(waypointInvader, transform.position, transform.rotation)as GameObject;
}
void Update()
{
if(go.transform.position != movePosition)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, movePosition, speed * Time.deltaTime);
go.transform.position = newPos;
}
}
}
Works great and all is good.
Now I’ve tried to do a little more with the script and get the object to move from its original waypoint, to a second waypoint.
new version script -
using UnityEngine;
using System.Collections;
public class InvaderWaypointSystem : MonoBehaviour
{
public GameObject waypointInvader;
GameObject go;
Vector3 movePosition01 = new Vector3(-75f, 1.7f, 65f);
Vector3 movePosition02 = new Vector3(-30f, 1.7f, 90f);
Vector3 movePosition03 = new Vector3(10f, 1.7f, 35f);
public float speed = 5f;
void Start()
{
go = Instantiate(waypointInvader, transform.position, transform.rotation)as GameObject;
}
void Update()
{
if(go.transform.position == go.transform.position)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, movePosition01, speed * Time.deltaTime);
go.transform.position = newPos;
}
if(go.transform.position == movePosition01)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, movePosition02, speed * Time.deltaTime);
go.transform.position = newPos;
}
if(go.transform.position == movePosition02)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, movePosition03, speed * Time.deltaTime);
go.transform.position = newPos;
}
}
}
I can see where its going wrong. Once the first if statement has been followed the object is at its destination and doesn’t need to go any further.
The object seem to be jittering so Im guessing its trying to move to the next waypoint, but the first if statement is keeping it an its position.
How do I make the object ignore the first if statement once it reaches that point and move towards the second… the third… and so on
???