- Home /
Help with making a map based game!
Hey im trying to make a game like occidental heroes but for pc, where the main screen is a map and you can click to move around and moving will cost food. I dont want the player to be able to walk on water and it will cost more food to walk on mountains. and im kind of a beginner.
the map:
Right now i have got a movement script:
void Update ()
{
if (Input.GetMouseButtonDown(0) && isWalkable == true && food >= reqFood)
{
player.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
but i can just walk around anywhere and i dont know how to do so the game knows how long it is to the destination so a reqFood will set!
Edit:I can now see the distance between the goal and player, but i cant figure out how to do so the cost increases if the distance is further away? and the walking on water part isnt solved either
Answer by $$anonymous$$ · Nov 07, 2018 at 12:21 AM
here how to do the not walking in water part
1)put a tag on the water call it water or something use OnCollisionEnter(Collision col)
void OnCollisionEnter(Collision collision){
if(col.collider.tag == "water")
{
isWalkable = false;
}
}
also this might help for the reqFood part or ai walking to destanation NavMesgAgent
@$$anonymous$$ine$$anonymous$$night10398 ive made a marker object that spawns wherever i click if in Walkmode. that marker shows where the player will move. i put your script on the marker object. ive made a collider on all the water areas and put it as trigger. but the isWalkable isnt turning false?? i have also tried OnTriggerEnter2D
try changing the Collision name to col like this
void OnCollisionEnter(Collision col)
so its
void OnCollisionEnter(Collision col){
if(col.collider.tag == "water")
{
isWalkable = false;
}
}
or the other way around if(collision.collider.tag == "water") { isWalkable = false; }
@$$anonymous$$ine$$anonymous$$night10398 i have done that, i also made a Edit at my question, please check it out :)
Answer by Aaqib_Zafar · Nov 07, 2018 at 05:35 AM
Add custom colliders, for each area,
Differentiate objects using tags,
Determine cost of each area,
Make player walk from one place to another and keep checking the area he is moving on using rayacst or collision , deduct the food amount as per the cost of area, 5.Don't move when he collides with water tag collider,
Hope it helps,
Answer by Matt1000 · Nov 07, 2018 at 11:11 PM
To know the reqFood you will need to know the distance you will cover.
Just do something like this:
Vector2 myPosition = transform.position;
Vector2 goToPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = goToPosition - myPosition;
//This is what you want
float distance = direction.magnitude;