Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by taylorjames9 · Jan 02, 2013 at 09:59 PM · movementmovegrid

Limit MoveTowards for one step at a time movement

Hi - I am making a click to move system in my game. Right now, I use the moveTowards function. When the player clicks (or taps) on a spot on the floor/grid, the character successfully floats to the point on the floor that was clicked (or tapped) by the player. So right now, the character can move one space, or 5 spaces - he just goes to the spot on the floor that the raycast hits. The problem is, while I want the player to be able to click anywhere on the grid, but I want the character to ONLY move one square in that direction.

Here is the code that I am currently using:

   if(floorHit){
      //transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
       transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);
              }

How would you recommend that I augment this line:

transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);

so that the character only moves to the next square on the grid in the same direction of the square that was clicked? It would even be alright if the character simply moved a limited distance in the direction that was clicked...something approximating a "step" in that direction.

Please help!

Comment
Add comment
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

3 Replies

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

Answer by Bunny83 · Jan 03, 2013 at 01:07 AM

I'm not sure what you mean by a step since you talked about a square and a grid...

So do you:

  • have a grid based game and want to move only horizontally / vertically and move only one grid when you click once?

  • don't really need to clamp the position to the grid and you just want a max-move-distance per click?

In both cases you should calculate a target the player object should reach. This would be calculated just once when you click, then you can use MoveTowards to move there.

In the first case the easiest way to determine the direction the player should move is this:

 var dir = targetPosition - transform.position;
 if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z))
     targetPosition = transform.position + Vector3.right*Mathf.Sign(dir.x)*gridsize;
 else
     targetPosition = transform.position + Vector3.forward*Mathf.Sign(dir.z)*gridsize;

This will set a new targetposition relative to the current player position and moves one grid (horizontal or vertical but not diagonal) to the next grid cell.

In case 2 you just need something like this when you click:

 var dir = targetPosition - transform.position;
 dir = Vector3.ClampMagnitude(dir,maxMoveDistance);
 targetPosition = transform.position + dir;
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
avatar image
0

Answer by Next Beat Games · Jan 02, 2013 at 10:09 PM

try this. Before you call: transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);

make a modification to targetPosition to make the player move a fraction of the targetPosition (you will have to determine this yourself).

so before the call, do this targetPosition = Vector3.Lerp (transform.position, targetPosition, 0.2f);

Comment
Add comment · Show 2 · 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 taylorjames9 · Jan 02, 2013 at 10:43 PM 0
Share

Thank you. That's very helpful! One follow up question: How would I create a variable that I can use to alter the size of a single step? Or to make the step a set distance rather than a fraction of the targetPosition Vector3?

This is the code I'm currently using:

   if(floorHit){
       targetPosition = Vector3.Lerp (transform.position, targetPosition, 0.4f);
       transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, speed);
  }
avatar image Next Beat Games · Jan 03, 2013 at 05:10 AM 0
Share

hmm its tough to give you a concrete answer when i dont know the nature of the game. it sounds like the game is tile-based, orthographic with a top down view. if thats the case, you may need to make a function that returns the center of the tile in the direction of the movement. then take that position and subtract it from your current position. i'll think about it more and get back to you. glad i was able to help! good luck!

avatar image
0

Answer by Maulik2208 · Jan 03, 2013 at 07:21 AM

 if(Vector3.distance(Gameobject1,Touchposition)==1)
 {
   /*Do your Stuff here*/
 }

This is a dummy code......try replacing the gameobject1 with your gameobject which you have to move and pass the values of touch by user in Touchposition(Both should be vector3)....and if the Distance between them is 1 as you have mentioned in your Question.....Put your code in i have not tested that but this could be a way to do this thing......Enjoy....If found useful then Don't forget to mark the answer.....

Comment
Add comment · Show 4 · 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 taylorjames9 · Jan 03, 2013 at 09:52 PM 0
Share

Thanks $$anonymous$$aulik2208. This is useful, but I still want the player to be able to tap anywhere on the board. No matter where they touch, the character only moves one space. I was able to make use of one the responses below, which I've marked as the answer. BUT HOW CO$$anonymous$$E I CAN"T HIT THE THU$$anonymous$$BS UP BUTTON TO VOTE ON USEFUL RESPONSES? The forum keeps telling me I don't have permission to do the thumbs action? What gives?

avatar image Bunny83 · Jan 03, 2013 at 10:46 PM 0
Share

It's because you don't had enough karma ;) UT implements this requirement to avoid spam on UA. We had massive spam here so the moderation queue was introduced and all posts from ppl with less than 15 karma need to be verified by any user with at least 1000 karma. This wouldn't make much sense if everybody can vote since a spammer could use 2 accounts and upvote himself.

avatar image Bunny83 · Jan 03, 2013 at 10:48 PM 0
Share

btw... @$$anonymous$$aulik2208:
It should be Vector3.Distance (capital D). Also the distance that is returned is a float value. It's almost impossible that it will be exactly 1.0

avatar image Maulik2208 · Jan 04, 2013 at 04:36 AM 0
Share

i know D is capital but i think you didn't see that i clearly mentioned that this is a dummy code means it will not work if copy and pasted......But no worries Guys.....i am glad with your suggestion and response.....Cheers......Enjoy....

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

11 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

Related Questions

How to Move an Object down until a certain point then activate a function 1 Answer

Help with Grid Movement 2 Answers

Moving AI away or towards player not working correctly. 1 Answer

I need help with some grid calculation for a digital board game. 0 Answers

Grid Movement VS Free Movement 1 Answer


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