- Home /
How to make an object to go from point A to point B and then from point B to point C
Hello, I just started to use Unity3D and I have to make an object to move through a room. Can you help me with the script for the object to move from point A to point B and then to point C? Also I tried like a couple of days ago but I'm getting stuck at some weird problem.. When the game object reaches point A(Empty Game Object) the camera starts to rotate back and forth.
There's tens of different ways to do this. Be brave and show us some code so we don't have to write an implementation from scratch
Answer by toddisarockstar · Jul 17, 2017 at 03:11 AM
public GameObject g;
Vector3[] points;
int index;
int i;
void Start () {
g = GameObject.CreatePrimitive (PrimitiveType.Cube);
points = new Vector3[3];
points [0] = new Vector3 (5, 5, 5);
points [1] = new Vector3 (-5, -5, -5);
points [2] = new Vector3 (4, -4, 0);
}
void Update () {
g.transform.position = Vector3.MoveTowards (g.transform.position,
points[index], Time.deltaTime*2);
if(g.transform.position==points[index]){index++;if(index>points.Length-1){index=0;}}}
Dont use Updatefucntion for this it is more performance consu$$anonymous$$g, rather use Coroutines: I hardly reccomend you to learn what is IEnumerator especially if u targeting mobile platforms
@$$anonymous$$enyus777 -- How much of a performance hit would you say it is?
In this case it is quite little but the Update function gets called in evryframe, while a coroutine only runs when u need it, in most optimization cases u start optimizing by reducing your Update functions, also use Profiler to check if in your case Update() is too performance consu$$anonymous$$g. In this particular case the movement is verybasic, and if it have to mvoes in evry frame well an Update function can be good too. But if you need to pause the moevement or change the movement, u need to set a shit tons of if Statements, and while/for functions. But with IEnumerator u can pause and delay the executions and Coroutines have virtually no performance overhead.
Using g.transform.position==points[index]
is one of the worst thing (Unity) developers can do. Don't you know that due to rounding errors, most floating-point numbers end up being slightly imprecise?
EDIT : The bare $$anonymous$$imum to test if two points are close to each other is to do : if( (g.transform.position - points[index]).sqr$$anonymous$$agnitude < _very_small_value_)
Please provide an alternative. It doesn't have to be an example if you don't have the time. Just give a few hints. Edit: Sorry, I didn't notice that you did provide an example.
Did you test Vector3.$$anonymous$$oveTowards() with that if-statement?
Answer by Hellium · Jul 18, 2017 at 10:52 AM
// Drag & Drop the gameobject which should move
public Transform myTransform;
// Drag & Drop all the GameObjects (empty) the transform must go to
public Transform[] positions;
private void Awake()
{
if( myTransform == null )
myTransform = GetComponent<Transform>();
StartCoroutine( Run() );
}
// Duration = The time needed for the transform to go from A to B
// WaitTime = The time the transform will wait between the "trip" from A to B and from B to C.
private IEnumerator Run( float duration = 5f, float waitTime = 3f )
{
int index = 0;
float time = 0;
WaitForSeconds wait = new WaitForSeconds( waitTime );
while( true )
{
Vector3 startPosition = positions[index].position;
Vector3 endPosition = positions[ (index + 1) % positions.Length].position;
for ( time = 0 ; time < duration ; time += Time.deltaTime )
{
myTransform.position = Vector3.Lerp( startPosition, endPosition, Mathf.SmoothStep( 0, 1, time ) );
yield return null;
}
myTransform.position = endPosition;
index = ( index + 1 ) % positions.Length;
yield return wait;
}
}
Answer by megabrobro · Jul 17, 2017 at 03:31 AM
or I was recently learning off some youtube channels, and they had some called 'Pathfinding tutorials' which is about how to set paths using trigger colliders. It was actually quite simple but sorry i dont know enough to actually post a description
Your answer
