- Home /
How to transform global position of an object
void Update () {
if (Input.GetButton("Horizontal") && Input.GetAxis("Horizontal") > 0) {
transform.Rotate(0,0,1);
transform.TransformPoint(new Vector3(-speed,0,0) * 2);
}
}
Here's my current piece of code.
transform.Translate() doesn't correctly move the assets that I wish to move.
As you can imagine, the object rotates, and as it translates relative to itself, it moves in an arc with the rotation. The solution to this in my head would be translating the global position of the object while rotating, which would eliminate the discrepancy.
TransformPoint seemed to be what I was looking for according to the Unity documentation, but the object I am trying to move remains in place.
Answer by HarshadK · Jul 17, 2017 at 06:50 AM
transform.TransformPoint transforms provided position from local space to world space it does not translate the object. As per Transform.Translate documentation, it states that:
If relativeTo is left out or set to Space.Self the movement is applied relative to the transform's local axes.
So if you want to move the gameobject in world space you need to pass the relativeTo parameter. Something like:
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
The above code will translate object by 1 unit in upwards direction in world space.
Your answer
Follow this Question
Related Questions
Make simple moving platform 2 Answers
Rigidbody Sink into BoxColider 0 Answers
Moving character on x axis 1 Answer