- Home /
How do I make my object move by exactly one in unity?
I've been trying to make a game similiar to the game "sokoban" (also known as "box pusher" to some people??) and I was wondering how I would make the box move by exactly 1 integer. so for example when I run into it, it moves from position 3 on the x to position 4 (or 2 if i push it left). I'm not sure how I'd do this though. I don't think the "addforce" or "velocity" functions would work. Does anybody have any suggestions?
use Vector3.MoveTowards, and make just modify the target position
Answer by SamElTerrible · Oct 14, 2021 at 09:50 AM
You're right, AddForce and Velocity are for physics, but you want to move things by a fixed number.
If you want to move a box by one unit on the X axis, you can do something like:
Vector2 newPosition = new Vector2(box.transform.position.x + 1, box.transform.position.y);
box.transform.position = newPosition;
Hey there @jonasAmeizing, thank you for this. by any chance do you also know how to detect if an object is ontop of another, such as if my player is on the same tile as the box?
Maybe you can calculate the distance between them using Vector2.Distance and if the value is less than something really small then that would mean they are in the same position? Or if you are moving our object by exact measurements you can just calculate the X, Y or the X, Z to the ones of your player to see if they are the same.
Hello there @luisquid, thank you for the suggestion, it really helped!
When you work with a grid / tile based game, the great advantage is that you can directly reference neighboring cells and check them. This is usually the main reason why games even used a fix grid because it simplifies everything.
Hey there @jonasAmeizing, the code doesn't appear to be working. Here is what I have
if(Vector3.Distance(Player.position, Box.position) <= 1f)
{
Vector2 newPosition = new Vector2(Box.transform.position.x + 1, Box.transform.position.y);
Box.transform.position = newPosition;
}
Hi @ReferenceWolf! Could you try making the value inside the if smaller? Something like 0.1f or such? That might help with your code implementation.
Hello there @luisquid, sorry for the late reply. This worked, thank you!
Answer by zORg_alex · Oct 16, 2021 at 01:28 PM
If you'd want to move it instantly, do like SamElTerrible wrote. If you want to do it smoothly, you could use LeanTween, just like that.
LeanTween.move(gameObject, transform.position + Vector3.right * 1f, .2f).setEaseOutBounce();
Should look neater with that bounce at the end. Also you could use this for your character animations, you could draw your own paths to follow etc.
And yes, I prefer to do
transform.position = transform.position + vector3.right * distance;
transform.rotation = transform.rotation * Quaternion.Euler(rotationInDegreesVector3);
var someVector = vecetor3.up * .5f;
for gameObjects translation and Vector3/Quaternion manipulation.
Answer by Trumped · Oct 18, 2021 at 01:09 AM
When ever you move your object make it snap, you can do this by using mathf.round on the X,Y,Z of the transform, this kind of thing works for tile based movement and building systems that's need to use a grid.
Your answer
Follow this Question
Related Questions
How can I make rigidbodies react cleanly to the character collider? 1 Answer
gameobject destroyed on collision with character 3 Answers
Primitive Collider slower then mesh collider 0 Answers
Pathfinding objects being forced to be far away from one and another 1 Answer
How to make a dynamic crowd? 0 Answers