- Home /
How to use transform.position?
I'm trying to create a turn based game, right now i'm just working on the players movement, heres what i've kind of come up with. using System.Collections; public float speed; public int playerCanMove = 1; public int oldPosition = Transform.position; void Start(){ if(Transform.position != oldPosition) playerCanMove--; } void Update () { if(Input.GetKeyDown(KeyCode.W)) { transform.Translate(0.0f, 0.0f, speed); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.A)) { transform.Translate(-speed, 0.0f, 0.0f); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.D)) { transform.Translate(speed, 0.0f, 0.0f); Transform.position = oldPosition; } else if(Input.GetKeyDown(KeyCode.S)) { transform.Translate(0.0f, 0.0f, -speed); Transform.position = oldPosition; } } void OnGUI(){ GUI.Button(new Rect(1200,600,100,100), "End Turn");{ playerCanMove = 1; } }
My question is, how can I properly use transform.position to implement this system? Should I even be approaching it in this way?
The multiple capital "T" in Transform looks suspect (as in Transform.position); if that's legal the logic is still weird cuz you are moving something with Translate (where the capital T is valid) then assiging the position back seems like it would stutter between the two
Essentially what i'm trying to do is subtract the moves the player can make by 1, each time the players position changes.
Answer by Regalith · Feb 22, 2014 at 01:03 AM
For starters always remember if you're moving something, always multiply it by Time.deltaTime so it'll remain constant on every end-system you're develop. Also you can use Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") for basic movement with alot less code. (this.transform.input.x += Input.GetAxis("Horizontal") * Time.deltaTime) As for the rest, what kind of turn based game are you making? Right now there's no real indication of what the constraints on movement are. For example, is it tile based movement? Whats the maximum a player can move in a turn? You need to consider all this when coming up with how you're going to design your movement system.
Its tile based, player can move just once per turn right now, but i'm going to multiply the 1 by some other stats later I think, such as playerCan$$anonymous$$ove =1 * sta$$anonymous$$a; something like that.
Well in that case you could do something like this
public int gridSize = 1;
public int sta$$anonymous$$a = 5;
public float speed = 2;
bool moving = false;
bool can$$anonymous$$ove = true;
void Update()
{
//$$anonymous$$ake Sure the player cant move while already moving
if(!moving && can$$anonymous$$ove)
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
$$anonymous$$ove(1,0);
sta$$anonymous$$a--;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
{
$$anonymous$$ove(-1,0);
sta$$anonymous$$a--;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
$$anonymous$$ove(0,1);
sta$$anonymous$$a--;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
$$anonymous$$ove(0,-1);
sta$$anonymous$$a--;
}
if(sta$$anonymous$$a <= 0)
can$$anonymous$$ove = false;
}
}
void $$anonymous$$ove(int xDirection, int zDirection)
{
moving = true;
while(this.transform.position.x != xDirection && this.transform.position.z != zPosition)
{
this.transform.position.x += $$anonymous$$athf.Lerp(this.transform.position.x, this.transform.position.x + xDirection, speed);
this.transform.position.z += $$anonymous$$athf.Lerp(this.transform.position.z, this.transform.position.z + zDirection, speed);
}
moving = false;
}
Hi, Thank you very much for this script i'm going to try to implement it later, currently it's giving me the error zPosition does not exist in the current context and I have to head to work.
it's just giving the idea of how you put together your code. I didn't debug it or spell check it, so just use it as a basis for building your own
Thanks man i figured that out and got it working. ill give you best answer since youve given me the most help.
Answer by ThomasJWolfe · Feb 22, 2014 at 12:29 AM
This will probably help. http://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate
These videos are a pretty good place to start scripting in general. They are short, but really quality info.