Salingship ai with addforce movement
Hello mates, İm working on enemy sailing ship.i wanna learn this steps but i dont know where to start ;
ship sail somewhere in the world (wandering) , in trade route or waiting somewhere
if distance between the player and enemy is close than some number, ( i can make it )
Chase the player,when close to player turn its sides to attack with cannons and make some organic movements to wait cannon reload and attack again.
im using ceto and realistic water physic.i create my player ship movement with addforce,so im trying to do enemy ai with addforce.i searched many times but i cant find pathfinding ship with buoyancy (waves shakes ship)
Pathfinding in a sailing game can be literally nothing if youre fighting on open water. You can have local avoidance using a overlapsphere to detect anything in range. Add a force in the opposite direction. Having a an AI ship that sails anywhere in the world seems unnecessary. What happens when you add 500 of them around the map. Are you going to calculate all of them at the same time? The player doesnt even know they exist, so why do they need to be calculated? You can come up with some clever tricks to simulate them traveling the world, but actually calculating their movement when they are nowhere to be seen, perhaps never seen, is unnecessary.
Physics overlapsphere to detect the player in an area add force in the direction to the player, if the distance is below X (using Vector3.Distance) then behave how you want.
Follow along with this series of articles : https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867
youll learn how state machines and ai work at a basic level. Youll need a state machine to act as the brain of your AI so it can make decisions based on the provided information.
I followed along and have created a little system for sharks and fish that swim around a node and eat eachother if hungry. They have local avoidance (explained in the articles) with wander, hunt, attack, and flee states.
Thanks for advices,yes calculating all the ship can be unnecessary but how i can move them soo,i dont understand this part: You can have local avoidance using a overlapsphere to detect anything in range. Add a force in the opposite direction.can you explain these to me?(im new to unity coding :)) im gonna create the states later.For now i just wanna make some movements that enemy follow player,turn its sides,moving around the player ship... like these photo;
First you would need to set up the wind controls so that the ships can move. How I am doing it (I am not using the "wind" that comes with unity) is selecting a direction for the wind by having a windcontroller, a script i made, that simple turns a transform to "lookat" the wind direction I want. https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
From there, each boat would have an angle check to compare its facing direction to the wind transforms facing direction. https://docs.unity3d.com/ScriptReference/Vector3.Angle.html
If the angle is right, apply force to the boat rigidbody in the transform.forward direction. The force can be scaled based off how aligned the boat is to the wind. (smaller angle = more force) https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
The enemy would need to do a check to see if the player is close enough. using Invoke repeating https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html and casting a sphere that collides with the players collider. If it finds the player, its first state would be to move towards the player. It would have a desired direction (to the player) and would attempt to steer itself towards. https://docs.unity3d.com/$$anonymous$$anual/DirectionDistanceFromOneObjectToAnother.html
using a distance check https://docs.unity3d.com/ScriptReference/Vector3.Distance.html it would deter$$anonymous$$e what to do from there. Say you wanted it to turn to the side to fire... you would first have to do another direction change and an angle check. If all requirements are met, attack.
For your enemy to know when one of its comrades has been defeated, it would need a sort of "squad leader" script to order the ships and update them on the situation.
As you can see, it isnt exactly an easy question. If you are unfamiliar with scripting, I would start by either learning how to, or using playmaker.