- Home /
Movement Position Being Equal to Int
I wasn't exactly sure how to title this, however I will explain what I mean. I am trying to make my game object move using:
transform.translate(Vector2.up * moveSpeed * time.deltaTime);
Now what I want to do next is what is causing some trouble. I would like the object to continue moving until it hits the Y value of 1, and then turn left. I almost have that working. See it WILL work if I use >= 1, because it skips over the value of 1 (not moving by choppy ints). Even if I try changing it to == 1.0f it still won't work as I guess the object is moving too quickly and skips over some values. We've tried re-positioning it in the >= block of code but that bugs out and doesn't work very well.
If you have any thoughts or possible solutions that would be great! Thanks! :-D
Code ex:
if (transform.position.x <= -2 && transform.position.y < -1) {
transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
}
else if (transform.position.y <= -4)
{
transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
}
look at $$anonymous$$athf.Approximately() ... and please, in future, format your code...
I'll look into it, and yes sorry just noticed now.. :-/ If there is a way to edit it I'll do so now.
I went ahead and just fixed it for you. Remember to use the 010101 button to format your code.
Answer by allenallenallen · Nov 04, 2015 at 06:43 AM
The solution you have is fine. It's almost impossible for the value to land perfectly as an integer.
Answer by Garazbolg · Nov 04, 2015 at 09:54 AM
It is normal behaviour but what you can do to correct that is : when you know your transform is above -1 just clamp it to that value like this :
if (transform.position.x <= -2 && transform.position.y < -1) {
transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
if(transform.position.y > -1) //Check if it has passed -1
tranform.position.y = -1; //Clamp it
}
else if (transform.position.y <= -4)
{
transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
}
This should fix your problem.
Your answer

Follow this Question
Related Questions
Vector-based Pong-ball bounce calculations 1 Answer
2D Sprite looks at the wrong side 1 Answer
2D Animation does not start 1 Answer
How Vectors work and how I should use them? 1 Answer
Prevent Player from sliding over edges 0 Answers