- Home /
Question by
Stormizin · Nov 06, 2013 at 01:05 AM ·
gameobjecttransformpositiontranslate
How to make an object go to pre-seted position in world
Anyone knows how to make a gameObject move to position that is already seted before? Something like that:
void Awake(){
objectPosition = new Vector3(10,10,10);
}
void Update(){
ObjectGoTo(objectPosition);
}
private void ObjectGoTo(Vector3 pos){
//Set the velocity to each x,y,z speed
float moveX = 2.0f;
float moveY = 2.0f;
float moveZ = 2.0f;
//Moving? #TODO: Go to parameter position
transform.Translate(moveX, moveY, moveZ);
//Here we check the limit of the position
if(moveX >= location.x){
moveX = location.x;
}
if(moveY >= location.y){
moveY = location.y;
}
if(moveZ >= location.z){
moveZ = location.z;
}
}
Comment
Best Answer
Answer by aldonaletto · Nov 06, 2013 at 01:13 AM
A simple way to move an object to a given position at constant speed is by using MoveTowards:
public Vector3 dest;
public speed: float = 2.0f;
void Update(){
transform.position = Vector3.MoveTowards(transform.position, dest, speed * Time.deltaTime);
}
Thank you, didn't notice that unity got a method like that. That helped a lot.