- Home /
How to move character a certain amount?
Hi there!
My character is currently moving with the use of Vector3.forward and Vector3.back.
He's placed as wished, but he's not "moving", he's teleporting. I've tried to get Lerps and MoveTowards to work, but I simply can't.
This results in the character ignoring collisions and moving further than expected, if an object is in the way.
What I wish to do:
When W is pressed, take time to travel 3.0f in the z-axis, instead of just being placed instantly. Also to check if there's an object in the way. If there is, return to current position in z-axis, which is 0. Same goes with pressing S, but moving -3.0f in the z-axis instead.
My current code looks like this:
private bool keyHasBeenPressed = false;
void FixedUpdate () {
if (Input.GetKeyDown(KeyCode.W))
{
if (keyHasBeenPressed == false)
{
transform.position += Vector3.forward * 3.0f;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.W))
{
keyHasBeenPressed = false;
}
if (Input.GetKeyDown(KeyCode.S))
{
if (keyHasBeenPressed == false)
{
transform.position += Vector3.back * 3.0f;
keyHasBeenPressed = true;
}
}
if (Input.GetKeyUp(KeyCode.S))
{
keyHasBeenPressed = false;
}
Any help is appreciated!
Thanks
Answer by Graham-Dunnett · Jul 11, 2014 at 09:55 PM
Can you elaborate on that :) ? I don't know how to utilize GetAxis