- Home /
Question by
Digital-Phantom · Apr 02, 2015 at 01:23 PM ·
transformpositionvector3moveinstantiated
How to move Instantiated object to pre-defined position(Solved)
How would I make an instantiated object move to a pre-defined position(transform).
I guess in simple terms I mean a single waypoint, nothing too technical to start with. just instantiate and move.
(I'll add multiple waypoints using arrays later, but for now simple is the goal)
???
Comment
Best Answer
Answer by HarshadK · Apr 02, 2015 at 01:33 PM
You can use Vector3.MoveTowards like below:
GameObject go;
Vector3 movePosition = new Vector3(10f, 10f, 10f);
float speed = 5f;
void Start()
{
go = Instantiate(prefab, transform.position, Quaternion.identity);
}
void Update()
{
if(go.transform.position != movePosition)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, movePosition, speed * Time.deltaTime);
go.transform.position = newPos;
}
}
O$$anonymous$$, just needed a slight adjustment to compensate for an error. Script now -
using UnityEngine;
using System.Collections;
public class WaypointBasic : $$anonymous$$onoBehaviour
{
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.$$anonymous$$oveTowards(go.transform.position, movePosition, speed * Time.deltaTime);
go.transform.position = newPos;
}
}
}
Works perfectly, many thanks @Harshad$$anonymous$$
:)