- Home /
Vector3.MoveTowards help :(
hi . i have a code like this and its very good :
var GO:GameObject; var GO2:Transform;
function Update()
{
GO.transform.position=Vector3.MoveTowards(GO.transform.position,GO2.position,Time.deltaTime*2);
}
But i wana make GO game object don't change in Y position . i wana GO game object just move for reach X and Z of GO2 !
excuse me for my bad english :) if have question , just ask :D
Answer by Pharaoh_ · Apr 09, 2015 at 02:13 PM
In the Start() method, set InitY = GO.transform.position.y; In the Update() method,
Vector3 newVec = new Vector3 (GO.transform.position.x, InitY, GO.transform.position.z);
GO.transform.position=Vector3.MoveTowards(newVec,GO2.transform.position,Time.deltaTime*2);
BUT a pronlem come up :(
when i use your code the GO.transform.position.x/z never equal with GO2.transform.position.x/z !!!!!
STRANGE !
numbers is like this : GO.transform.position.x = 0.09 and GO2.transform.position.x = 0.1 !!!! they never reach together !
but with my code they are equal , like this : GO.transform.position.x = 0.1 and GO2.transform.position.x = 0.1
I may be misunderstanding the question but that looks wrong to me. It can change the y coordinate of GO (because it's moved towards GO2's position). You want to move GO towards a point that has its y coordinate but GO2's x and z. So something like..
Vector3 newVec = GO2.position;
newVec.y = GO.transform.position.y;
GO.transform.position = Vector3.$$anonymous$$oveTowards( GO.transform.position, newVec, speed);
Thank You So $$anonymous$$uch BonFire-Boy . i close this question but i vote your answers up 3 time :) :D you solve it :)
Your answer
