- Home /
Turn and Move Foward 1 Unit relative to Objects local direction
Hello,
How can I modify this section of my script so my game object moves foward 1 unit relative to its rotation angle?
In this part of the script my object suppose to turn right and move forward 1 unit base on trigger events.
Every "right turn" will be relative to the game object's new position/direction from any previous right turn. So every trigger will set off an additional 90 degree turn from the last rotation.
Instead my object turns right and my object does not move foward in the Objects local direction but in the direction of the newVector 3(1,0,0) which is from what I understand always a constant World direction no matter which way your game object is facing.
// Turn to the right
print("right");
localEulerAngle = new Vector3();
localEulerAngle.y = 90;
thisTransform.eulerAngles = localEulerAngle;
// Set the new target position to move to
targetPosition = thisTransform.position + new Vector3(1, 0, 0);
// Make sure the player lands on the grid
targetPosition.x = Mathf.Round(targetPosition.x);
targetPosition.z = Mathf.Round(targetPosition.z);
Thanks in Advance.
most likely you want transform.forward or transform.right
Answer by SirAstral · Jul 15, 2015 at 12:50 AM
Here is something I tested.
// This will rotate the object to the right 1 degree. If you want it to rotate so that it faces right you would use 90 where the 1 is at. to turn it left put a - in front for minus.
this.transform.Rotate(new Vector3(0,1,0));
// This will move the object forward which is by default its Z axis.
this.transform.position = this.transform.position + (transform.forward + new Vector3(0,0,1));
If you put both of these together in the void Update () method you will see the cube go forward as it turns to the right. This code will move your object how you are wanting. But you will have to figure out what logic calls this movement.
Your answer
Follow this Question
Related Questions
Trouble converting transform.position to C# 1 Answer
How do I change one value of a vector? 2 Answers
Euler Angles and Vectors 3 Answers
fixed distance for Vector3.moveTowards ? 1 Answer
How to anticipate the Vector3.forward of the enemy? 1 Answer