Rotate object after teleport in the direction of next position

Hello :slight_smile:
Think I have gotten more into it but still got some things i get stuck on

So the problem is the following.

1.)I teleport my unit (like on the picture below
2.) He should not be able to pass through the door (i call seeker.CancelCurrentPathRequest():wink:
3.) I want the unit to ROTATE only towards the door.

I have tried the below code but this just moves unit a “lil” towards the position but doesn’t rotate him (which is kinda expected behavior as canMove disables all type of movements and i guess it takes him some time to realise that moving is disabled, like a frame)

Tried (GetComponent().updatePosition = false; instead of canMove) just for test and this does disable movement but, unit rotates towards the door fully and then rotates back to original direction? Can i make him stay oriented :slight_smile:

Is there some better way that I am missing? (most probably since this doesn’t seem right)
(using AILerp)

                            ThisIAstarAi.canMove = false;
                            Vector3 nextPosition;
                            Quaternion nextRotation;
                            // Calculate how the AI wants to move
                            ThisIAstarAi.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
                            // Modify nextPosition and nextRotation in any way you wish
                            // Actually move the AI
                            ThisIAstarAi.FinalizeMovement(nextPosition, nextRotation);

image

P.S. Just to reiterate, I have everything solved when unit is moving TOWARDS the door from at least 1 tile away (as it will be facing the door once it walks in on the tile), the problem is when i use teleport, as the unit is on that same tile as the door and is facing opposite direction i am not sure how to turn him towards the doors. (i could obviously rotate him myself but would like to do it through the system so everything is consistent)

Typically I handle rotation like this:

    public static void FaceTarget(Vector3 targetLocation, Transform transform)
    {

        Vector3 direction = (targetLocation - transform.position).normalized;

        if (direction != Vector3.zero)
        {
            Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
            transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
        }
    }

I’m not sure if it will work exactly for your situation, but I thought it might help.

2 Likes

Hello,

Yeah this is perfectly valid solution in term of general rotation, but the point is that it is “outside” handling so it leads to “messy” code. (For example i need to make some checkup if unit is moving and then cancel this movement if i continue moving using A* pathfinding).

Which is not impossible by any means but if there is a solution that can still continue using pathfinding alghoritms it would be preferred. (As you get all the native rotation speed and what not which is already calculated into the alghoritm)

Thank you tho might use it if it is my only choice (as it is then the only valid choice) :slight_smile:

i mean, there are options to disable rotation and/or movement, there must be a way to to make this work, I am just not seeing it myself :frowning:

RESOLVED (can’t seem to be able to edit title into resolved so hope this does it)

Managed to solve the issue.

Basically (to try and help if smn else gets stuck so aron has a bit less work :P)

1.) seeker.StartPath (transform.position, newPosition, OnPathComplete);
2.) inside, I gather data about my blocked doors and where they are.
3.) In case i am on the slot i can shoot from i override my path so my unit doesn’t go through the open door (I will say this part is a bit “fishy” logic wise to me but it works perfectly so will check it out tomorrow a bit more and update if i find any problems)

var customPath = ABPath.Construct(transform.position,slot.worldCoordinates);
ThisIAstarAi.SetPath(customPath);
ThisAIDestinationSetter.SetTargetDestination(slot.worldCoordinates, null);

4.) Stopping the unit, cancelling the path or anything of the sorts is super bad as unit “halts” and then it takes him some time to recalculate the path. This does it without any problem at all.

5.) This part is important as it actually waits until unit is on the path.

 private IEnumerator ShootAtPositionWhenReachedPath ()
    {
        yield return new WaitUntil(()=>ThisIAstarAi.reachedEndOfPath);
        ShootAtPosition();
    }

6.) I “stole” a bit of aron’s code for rotation, just so I can keep it consistent with my own for rotation (and i must admit it is pretty nifty way of doing the rotation, shorter than the solution i had.)

    void RotateTowards()
    {
        Vector3 direction = (_invaderAnglePosition - ThisTransform.position);
        Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.back);
        targetRotation *= Quaternion.Euler(90, 0, 0);
        ThisTransform.rotation = Quaternion.Slerp(ThisTransform.rotation, targetRotation, Time.deltaTime * aiLerp.rotationSpeed);
    }

And that is it :slight_smile: My units now stand on tiles, inside rooms, separated by doors and shoot towards the doors. Gl and hope it helps someone. Cheers!

RESOLVED

1 Like