Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ReferenceWolf · Oct 13, 2021 at 07:27 PM · physicsboxpushing

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Oct 14, 2021 at 06:27 AM 0
Share

use Vector3.MoveTowards, and make just modify the target position

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ReferenceWolf · Oct 15, 2021 at 07:53 PM 0
Share

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?

avatar image luisquid ReferenceWolf · Oct 15, 2021 at 11:46 PM 0
Share

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.

avatar image ReferenceWolf luisquid · Oct 16, 2021 at 12:28 PM 1
Share

Hello there @luisquid, thank you for the suggestion, it really helped!

avatar image Bunny83 ReferenceWolf · Oct 16, 2021 at 12:32 AM 0
Share

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.

avatar image ReferenceWolf · Oct 16, 2021 at 01:44 PM 0
Share

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;
         }
avatar image luisquid ReferenceWolf · Oct 22, 2021 at 07:17 PM 0
Share

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.

avatar image ReferenceWolf luisquid · Oct 26, 2021 at 01:16 PM 0
Share

Hello there @luisquid, sorry for the late reply. This worked, thank you!

avatar image
1

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.

asset store link

     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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zORg_alex · Oct 16, 2021 at 01:34 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

214 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges