- Home /
Move along grid where facing
I've been trying to make a dungeon crawler, like Legend of Grimrock, and I can't get the movement right. I can't even get "snapping" movement right. Is there a way I can add 1 to the x or y (depends on where you're looking)? Or even better, slowly move the camera until it has added 1 to the vector. Here is my try...
function Update () {
if(Input.GetKeyDown(KeyCode.W)){
transform.localPosition.z += 1;
}
if(Input.GetKeyDown(KeyCode.A)){
transform.Rotate(0,-90,0);
}
if(Input.GetKeyDown(KeyCode.S)){
transform.localPosition.z -=1;
}
if(Input.GetKeyDown(KeyCode.D)){
transform.Rotate(0,90,0);
}
}
As you can see, I have only managed to snap angles, and snap only to one vector. Help?
Answer by robertbu · Jun 28, 2013 at 06:26 PM
If you want to move one unit forward you can do:
transform.position += transform.forward;
This will move the camera forward one unit since transform.forward is normalized (i.e. one unit long). To move backwards:
transform.position -= transform.forward;
As for moving it over time, that is more complicated. Look at Vector3.Lerp() and Vector3.MoveTowards() for two functions that move things over time.
Your answer
Follow this Question
Related Questions
Smooth Camera Movement script with problem. 0 Answers
How to make sliding room transitions like in Mega Man? 1 Answer
Edit SmoothFollow.js to change height and distance vars smoothly on some triggers 1 Answer
What is the best way to make smooth camera movement and rotation? (with my script) 1 Answer
Smooth Attached Object movement 1 Answer