- Home /
Moving player 1 unit from current position.
I'm just trying to get my player model to move 1 unit, the direction doesnt matter but in the end i'll want to be able to move up one down one left one and right one. But I also want there to be a limited number of moves you can make before ending the players turn and resetting the number the player can move.
Biggest problem i'm having is that I don't know how to call the players current position.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Vector3 playerPosition;
public int playerCanMove = 1;
void Start () {
playerPosition = transform.position;
for ( playerCanMove = 1; playerCanMove >= 1; playerCanMove-- )
{
if(Input.GetButtonDown("Vertical"))
{
playerPosition = (new Vector3(transform.position.x,transform.position.y + 1, -1));
}
}
}
}
If someone could explain to me how to do this i'd be very thankful.
This is in 2d so I have the z axis at -1 to bring the player to the foreground.
Answer by GameVortex · Feb 17, 2014 at 06:34 PM
You can easily use **Transform.Translate** to move an object:
//moves object 1 unity to the right
transform.Translate(1.0f, 0.0f, 0.0f);
omg, thats exaclt what I was looking for. any idea how to make the transform more smooth? (with your line of code the player is simply "teleporting" to the new spot...) this is fine, but it would be cool if it was a "smoooooth" transecation.
Answer by whydoidoit · Feb 17, 2014 at 06:43 PM
Vertical is an axis you get with GetAxis and has a value between -1 and 1 with 0 being at rest. If you want to do this on a button use KeyCode.X (or any other key you like).
Your answer
