hi i am making uh game where player chase object try and catch it. i have trouble with ai for object to run away. here is tricky part. i would like to have ai face enemy as actually it move backwards as player get closer it trys to run away. facing player is not uh problem, but how to get it to move in backwards direction? sorry if i not make much sense. here is idea overall. animal stalks player (working) but keeping uh distans=ce (working) player try to chase animal animal back up still facing player. this part not working. after too much, animal turn around run away to try make sure player not get too close yes? working. see? sorry again! thank you!
Find the Vector from the player to the NPC and move in that direction. Code (csharp): Vector3 moveDir = npc.transform.position - player.transform.position; transform.Translate(moveDir.normalized * speed * Time.deltaTime);
not tested but must(can) be like this: Code (csharp): float amount; public GameObject player; Vector3 direction; Vector3 runPoint; Vector3 walkPoint; float speed; void Start(){ runPoint = transform.position; walkPoint = transform.position; } void Update(){ amount = ((player.transform.position)-(transform.position)).magnitude; direction = ((player.transform.position)-(transform.position)).normalized; if(amount>100){ // if player is more then 100 units away normal(); // normal waypoint system or what you want } else if(amount>20){ // player is beetwen 20 and 100 units away chased(); // animal has chased the player } else{ // player is less then 20 unity away run(); } } void normal(){ if((walkPoint-transform.position).magnitude<5){ // get new point here } else{ // moving script here } } void chased(){ transform.LookAt(new Vector3(player.transform.position.x,0,player.transform.position.z)); transform.position+=-direction*speed*Time.deltaTime; } void run(){ if((runPoint-transform.position).magnitude<5){ } else{ // moving script here } } this is how i would solve this problem you just have to write those lines to get new point with your conditions and the walkstuff( dont know if you are doing this with RB or CC or maybe something else? i didnt test the code, so it may have bugs
dart: thanks for answer! from look at code this moves animal toward player no? will try in uh bit but looks similar to what i use for animal to come to player. zero_quantum: thanks for answer! i think you explain how to decide if animal stalk player or run away. it is little different from how i set up, sorry i did not give code example. what i am looking for is actually how to reverse the direction the animal is walking so instead of going toward the player it goes awway from the player while still looking at the player. vector3 my code now finds vector3 between player an animal and moves to player. i wonder how to reverse direction so animal move away from player. thanks again all your help! i really appreciate any response!
well my script already does it: transform.position+=-direction*speed*Time.deltaTime; thats the line -direction or +direction depends on how u defined it. if your direction equals player.position - animal.position you have to take -direction if you give me your code i can tell you what you have to change...
both of these work! i am sorry. thank you so much! darts is slightly wrong. its player.position-npc.position instead of other way. smooth movement good! zero work good too, except moves is not smooth but chunky. i think both do not have gravity and collision for move through walls. animal should move uh lon side wall. i use ai script for move now from fps demo shooter from unity for move forward. try to use same for move away but do not know how to get working! i should post as example someone can help maybe fix? i only wonder because if better this way ok but if zero or dart are right on uh answer i will use that! sorry- just read zero say give code and will fix! i will do this! have to get laptop back from brother first. thank you!