Rotate AI

Hi i am creating an underwater survival game with animals. I am trying to make my AI shark rotate to face Other AI (fish) when in a certain distance but so far i have had no luck i am using mercuna and behavior designer and so i am not using a Navmesh. If anyone could help please here is my rotation script

 public override TaskStatus OnUpdate()
        {
            Vector3 direction = target.position - transform.position;
            Quaternion rotation = Quaternion.LookRotation(direction);
            transform.rotation = rotation;
            transform.Rotate(Vector3.right * speed * Time.deltaTime);

            {
                return TaskStatus.Success;
            }
   
        }

        // Return targetPosition if targetTransform is null
        private Quaternion Target()
        {
            if (target == null || target.Value == null)
            {
                return Quaternion.Euler(targetRotation.Value);
            }
            var position = target.Value.transform.position - transform.position;
            if (onlyY.Value)
            {
                position.y = 0;
            }
            if (usePhysics2D)
            {
                var angle = Mathf.Atan2(position.y, position.x) * Mathf.Rad2Deg;
                return Quaternion.AngleAxis(angle, Vector3.forward);
            }
            return Quaternion.LookRotation(position);
        }

        // Reset the public variables
        public override void OnReset()
        {
            usePhysics2D = false;
            rotationEpsilon = 0.5f;
            maxLookAtRotationDelta = 1f;
            onlyY = false;
            target = null;
            targetRotation = Vector3.zero;
        }
    }

Can you tell us a bit more what is not working right now?

Since you are using BD, the Scripting is slightly unfamiliar and presupposes knowledge of BD. From what I dimly remember, BD stops evaluating when you return success. You never return failure, but immediately succeed after a single turn toward target. There is no distance Evaluation anywhere. But again, you are using a third-party tool for the main logic, which makes spotting issues rather opaque. For example, it’s unclar to me when Target() is invoked.

my shark is spotting a target (game object i have set ) but not rotating properly it basically just turns on a pivot sharply instead of a smooth transition. I have it set to turn before pursuing but this is unfortunately turning the shark (sharply) even if it is facing the AI fish. I’m trying to get it to swim then when a AI fish swims to close to its behind or swims to close from above and below, i want the shark to turn to face them them ?

Yes, that’s how you wrote the code in OnUpdate, and you immediately return Success so BD will go to the next node. You’ll need to revisit this: you need to turn and not return anything until the shark points almost directly at the target (i.e. the angle between target direction and forward are less than 5 degrees).

That being said, you are using mercuna - IIRC that’s a $2000-$8000 asset that should be able to provide you with a spline path to follow. Why are us using BD for this?

i am using bd to allow my shark to pursue and and do other behaviors such as within distance as mercuna is just for movement. My sharks are set on a random wander until they see the object ?

This will probably help:

I just used this recently to get a swimming character to turn and face the direction that they were moving in.

ah right will give that a whirl thank you :slight_smile: is their any way i can use nav mesh on this type of game as when i tried it the sharks don’t swim smoothly or at different areas (e.g low to the ground and high to the surface of the water)?

The problem with the navmesh, is that it’s pretty-much just limited to 2D planes. It’s not really good for swimming or flying that can take place in an entire 3D volume.

Edit: or I should say Unity’s navmesh. I’m sure it’s possible to develop a 3D navmesh, but that’s not a built-in feature.

ah right is their any other way to doing this without using mercuna ?

I don’t think Unity has any 3D navigation features built in, so you’d either have to write your own system or use a different plugin. I’ve not used any 3rd-party plugins for 3D navigation so I don’t know of any good ones to recommend.

I just happen to be working on an underwater scene in my game too, so I’ll need to tackle this challenge myself pretty soon. I’m considering getting one of the inexpensive “flocking with obstacle avoidance” packages on the Asset Store (just to see how these products handle obstacle avoidance in a performant way) but I haven’t decided on anything yet.

I’m just curious, though- What problems are you having with Mercuna? I’ve never used it before, but the website makes it look pretty high-end.

ah right. Everything works perfectly in mercuna but only based on my sharks being able to avoid obstacle and wander around so that looks good but it is trying to integrate other plugins which seems to be an issue. I have looked at the link you posted earlier about rotation how would i go about adding a set target so that it only rotates when the fish is behind the shark?

I’d be surprised if Mercuna didn’t have a built-in way to to rotate the agent in the direction that it’s moving, but aside from that, does Mercuna have a scripting API for Unity? I would imagine it would have a function that would let you get the current direction that the agent is heading. I think that’s all you would need.

I believe so and they have a few nodes such as seek and move to which it will move and pursue if the other agent is in front i just can’t find how to rotate around to face object. I have a “can see” bd node which the pursue and attack respond to but it only displays the purse and does attack it still wont rotate to the object and can’t find anything on this for mercuna ?