Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple point and click script, for player movement.

When I click on the screen my character Moves towards where I clicked.
and plays the walk animation, this is all good. when he arrives at the clicked position he is just stuck in walk animation on the spot.

How do I say when the object arrives at the position of the click then //Do somthing else// ie play stand animation?

What I have tried:

public float speed = 1.5f;
private Vector2 target;
 
void Start () {
    SkelAnim = GetComponent<SkeletonAnimation>();
    target = transform.position;
     }
     
void Update () {
    if (Input.GetMouseButtonDown(0)) {

    SkelAnim.state.SetAnimation(0,walk,true);
    target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    
         }
    transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    

    }
   }    
Posted
Updated 18-Feb-19 23:21pm
v2
Comments
OriginalGriff 19-Feb-19 4:17am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 14135033 19-Feb-19 4:37am    
Thanks for replying Griff, so on mouse click, I have the character walking to the point of position, I'm trying to figure out how I'm going to say when he arrives at the clicked point, Do something else
OriginalGriff 19-Feb-19 4:44am    
OK, so how do you know where that point is? How far away is "at the point"?
What have you tried so far?

(Sorry if this sounds like stupid questions, but we really can't see your screen and there are thousands of different ways this could be working!)
Member 14135033 19-Feb-19 4:58am    
I guess that's why I'm confused, I don't know how best to do this. I'm detecting that point to click (ScreenToWorldPoint) Input.mousePosition.

This is the only code I have tried that has worked for me to get him to walk to point of click.

1 solution

Quote:
I guess that's why I'm confused, I don't know how best to do this. I'm detecting that point to click (ScreenToWorldPoint) Input.mousePosition.

This is the only code I have tried that has worked for me to get him to walk to point of click.


No, not "how does you code know where that point is" - if you could answer that you wouldn't have to ask the question! :laugh:
How do you know? If I tell you "go to 14, Treacle Mine Road" and you set off on foot, how do you know you got there?

Well, basically you compare your current position with the required destination and when the difference is zero (or very small) you have "arrived". Translate that into a grid, and you are on cell (4, 2) and 14 Treacle Mine Road is on cell (16, 7). Are you there? No - because 4 isn't the same as 16, and 2 isn't the same as 7. So you walk in "an approved direction" (up, down, left right, maybe diagonals) by modifying your current cell address until you arrive. If 16 - 4 is bigger than 7 - 2, add one to your X to move to (5, 2), otherwise add one to your Y and move to cell (4, 3). Check again. Move if you aren't there.

Does that make any sense?
 
Share this answer
 
Comments
Member 14135033 19-Feb-19 5:33am    
Thank you, that makes better sense, I need for him to have coordinates of position clicked, and then check if those coordinates are = to, to know he has arrived at Treacle Mine Road :)
OriginalGriff 19-Feb-19 5:40am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900