- Home /
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!
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;
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);
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);
}
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!
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.....
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?
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.
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
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....