- Home /
Translating an object to another position, help!
Hey!
Now this is supposed to be very easy, done it with OpenGL and I don't get what i'm doing wrong.
So I have this object with its coordinates given on the UI (world coordinates??) and I have another set of coordinates in which the object should be on after an event. Also these coordinates were acquired using the editor and just reading the coordinates on the UI. So how should it be scripted, which one of the rows below is the right way?
transform.Translate(X,Y,Z); //Realtive change?, what should the coordinates be?;
transform.position = Vector3(X,Y,Z); //This doesnt work either, probably because I dont know much of the basics
Also saw some TransformDirection and InverseTransformDirection but i'm guessing those are used for changing between spaces..
Answer by testure · Jun 06, 2011 at 06:39 PM
personally, for something like this I just Mathf.SmoothDamp (or Vector3.Slerp) and handle the position change manually.
void Update() { transform.position = Vector3.Slerp(transform.position, desiredPosition, speed * Time.deltaTime); }
So the "desiredPosition" would be the acquired one from the UI?, Tried that before and i just can't get it to change position. Don't need any interpolation or keyframe animation either.
Guess it was this I was looking for: "It is worth pointing out that the Transform values in the Inspector of any Child GameObject are displayed relative to the Parent's Transform values. These are also called the Local Coordinates. Through scripting, you can access the Global Coordinates as well as the Local Coordinates."
acquired from the UI? no.. you'd script it in whatever way you needed.. IE: player input, or external motivation.. whatever you need. or i guess you could acquire it from the UI but I don't see much point in that.
if you don't need interpolation, then just set transform.position to whatever Vector3 you want. It'll move instantly and you're done.
But that is the point! it wont get to the position I want to and I wonder why is it so
in your update function, if you (for example) add:
transform.position = new Vector3(20f, 10f, 15f);
and it doesn't move- then you have a problem somewhere else in your code. manually changing the position of your game object in script automatically takes precedence over everything else- including UI input. If you still can't get it working using the information I've given you, post your full script so we can see where the problem is.
Answer by ramp · Dec 27, 2012 at 10:23 AM
What you are looking for is something like: transform.position = Vector3.Slerp(transform.position, gameObje.transform.position, 5 * Time.deltaTime);
Your answer

Follow this Question
Related Questions
Position Error in p30 lite - android 10 0 Answers
how to set rotation / position of an object on trigger? 2 Answers
How do I translate around a circle? 3 Answers
Stop moving at the target position by using Vector3.forward 0 Answers
2D get touch input 1 Answer