Since I am horrible at coding I’ve decided to keep the script that controls my player and the script that controls movement as two separate scripts, both attached to my player.
To make the AStar function the way I want it to, I put everything inside the Update () function inside of a raycast function, within an Input.GetButtonDown function. This made it so nothing happens with the movement until I click somewhere on my level. So far this works perfectly and I have no issues with it.
My second script, which holds all my player data and includes its own raycast within an Input.GetButtonDown function so I can get the data I need from the object I click on. It seems to me that having two raycasts firing every time I click to get essentially the same data is wasteful. I intend to keep the scripts separated because of the length and complexity of the AStarAI script. I also need to control if the player is moving or not at different times, so having the player always move when I click is not a good idea.
I am trying to figure out the best way to still run the AStarAI script when I click on the ground, but only have one raycast to catch all the data, preferably on my player script. Is there a simple way to make this work? Is there somewhere I should look for help?
Hi
I would suggest that you make a method in one of your classes named something like MoveTo (or something similar) which takes a RacastHit or a Vector3 as the input and does whatever it needs to do. One of the scripts will call the other script.
See
Also, you should know that there are included movement scripts so you do not need to write your own in many cases (see the AIPath script).
That sounds simple enough, I’m guessing I make a MoveTo () function in my player script and when I use MoveTo it calls a raycast to get the data. Then send the appropriate data to the AStar script using the getComponent.
I’ll give it a whirl and see how it goes.
Those links are useful!
I was not aware of that. I’m still trying to figure out all this code so I’ll see what I can do.
Thanks!
Ok I got it to send the data properly but I have some small issues.
First, when my player reaches his destination there’s an endless cycle of printing that the player has reached his destination.
Second, as soon as I start the game the player begins moving. I don’t want the player to move until I’ve sent the data. I’ll try starting the Vector3 as null and see if that works.
Third, there seems to be significant lag between when I click and when the player starts moving. I’m pretty sure its because every time I run my MoveTo function it is searching for the AStarAI script with the GetComponent. Is there a way to set the script in my variables? Or somewhere else so I don’t have to keep searching for it every time I click?
Makes sense. I’m guessing I have to store that bool in the AStarAI script though. Would I check for that bool at the start of the Update function then? That way its only searching if it hasn’t reached its destination.
I’m only requesting MoveTo when I click. I believe its the way the AStarAI script is set up, since it all runs durring the update it will immediately start working as soon as the first update is called. I just need a way to either gate the start up or change the start Vector3
Hm I’ll take a look at it again and see what I can find