- Home /
Question by
aliwalid · Jul 24, 2020 at 09:14 AM ·
c#unityeditortransform.positionif-statementspostion
if player transform position x do something
I have a gameobject and It moves and I want to make it whenever it moves do something but I can't. this is what I tried to do:
if (player.transform.position.x) { do something
}
what should i do
Comment
Answer by Llama_w_2Ls · Jul 26, 2020 at 08:38 AM
GameObject Player;
Vector3 LastPosition; //Updated every second
private void Update()
{
if (Player.transform.position.x > LastPosition.x ||
Player.transform.position.x < LastPosition.x) //If my position has changed from my original position:
{
//something
}
StartCoroutine(UpdateLastPos());
}
IEnumerator UpdateLastPos() //Sets a control value of the players movement every second
{
LastPosition = Player.transform.position;
yield return new WaitForSeconds(1); //delay of 1 second
//This, when compared with the constantly updated player transform,
//will detect if there was a change in position. The delay doesnt really have to be 1 second.
}
Not sure if this will work
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Full Screen Toggle will not work. 1 Answer
Checking the color of game objects with specific tag. 2 Answers
Custom Unity Editor Issue 1 Answer