#Not sure if I knew the exact term but

1 messages · Page 1 of 1 (latest)

nocturne hornet
#

I make a thread as to not monopolize the conversation

earnest flume
#

Fair enough

nocturne hornet
#

so the thing is when your character move, there is two way to express his movement

#

either you look at its coordinates in Unity, and describe their changes

#

this is what we call a global frame of reference

#

it's global because everyone has the same origin

#

the same (0, 0)

#

but you could also describe it according to the player. If the player move 1 unit in front of them this vector would be described as (0, 0, 1) in their local frame of reference

earnest flume
#
        _oldPosition = transform.position;```

Would this be local frame of reference?
nocturne hornet
#

no, this is global. transform.position is always global

earnest flume
#

But using localPosition will give the same result if it's no childed, right?
Or am I thinking wrong

nocturne hornet
#

localposition gives you the frame of reference of the parent

earnest flume
#

Ahh

nocturne hornet
#

if there are no parent then yes it is global

earnest flume
#

Sorry, been working for most of the day already, so not as sharp as in the morning 😅

nocturne hornet
#

yeah linear algebra took me some time too dw :)

#

but see it this way. Imagine both your player and enemy move forward. They would have very different vectors in the global frame, but in their respective frame of reference, they both move along (0, 0, 1), right ?

nocturne hornet
#

ok, so what you need to do is take the local movement of the player, give it as is to the enemy, flip it, then have your enemy perform this movement as if it was in his local frame of reference

does it makes sense to you ?

earnest flume
#

I think so yeah

#

MovementPerFrame = transform.InverseTransformVector(transform.position - _oldPosition);
_oldPosition = transform.position;

This seems to give the right value for the player movement

nocturne hornet
#

Yes, it would give you the player's movement in the local frame of the player

earnest flume
#

_trackers.position += transform.TransformVector(_playerInfo.MovementPerFrame);

This mirrored correctly, so now I just gotta flip x

nocturne hornet
#

either x or z

#

I think it's z

#

anyway, gotta go now, good luck

earnest flume
nocturne hornet
#

👍

earnest flume
#

And learned some more linear algebra for games haha

nocturne hornet
#

linear algebra is the single most useful theory of maths when it comes to game dev

earnest flume
#

Ah shoot this almost worked

#

(dont feel pressured to respond btw)

#

Now when the player rotates so the forward is to the left of the enemy and moves forward the enemy moves to his forward instead of mirroring the player

#

The straight lines are the path I think. The dotted line is the desired path

#

The code rn would have worked if the player also would be forced to look at the enemy I think

earnest flume
#

Trying something to take into account the angle ```cs
var movementPos = transform.TransformVector(_playerInfo.MovementPerFrame);

        var newAngle = Vector3.Angle(-_trackers.forward, _player.transform.forward);

        var newVector = Quaternion.AngleAxis(newAngle, _trackers.up) * movementPos;

        _trackers.position += newVector;```

Bit better I think but not fully

wooden ferry
#

Use syntax highlighting

var movementPos = transform.TransformVector(_playerInfo.MovementPerFrame);

            var newAngle = Vector3.Angle(-_trackers.forward, _player.transform.forward);

            var newVector = Quaternion.AngleAxis(newAngle, _trackers.up) * movementPos;

            _trackers.position += newVector;
wooden ferry
# earnest flume Got it

Can't help with your issue though, not good with math, but I just wanted to say that you should apply syntax highlighting :P
(It helps for people who can help with the problem, readability is better :) )

next scarab
earnest flume
#

Have an enemy behave like you are looking in a mirror basically

next scarab
#

There is many ways of “Mirroring”. So you want axial symmetric movement?

next scarab
earnest flume
#

But there is no 1 axis, since the player can freely move. The enemy will procedurally spawn

next scarab
#

So you want your movement vector symmetric, not position.

earnest flume
#

What do you mean with that?

#

Sorry had math in dutch all my life so the terms are sometimes a bit fuzzy

next scarab
#

I will come back and explain soon

earnest flume
#

Sure!

next scarab
#

Where the blue is player and red is enemy

earnest flume
#

I think all enemys should have a 'mirror' between them and the player.
So if you look at the enemy at the bottom right and walk towards it, it will also walk towards you

#

If that makes sense

nocturne hornet
#

re

nocturne hornet
#

so then if the player walks toward the enemy, it is counted as "forward"

earnest flume
#

So if you'd face an enemy and will walk into the enemy if you try to pass him

#

Fair enough, maybe I can figure out something like that next week

nocturne hornet
#

you can simulate a "mirror" by reflecting the position either around an axis or a point

earnest flume
nocturne hornet
#

ok, so how to reflect a position around an axis

earnest flume
#

The top right will go towards the player arrow

#

Then yes

next scarab
#

Oh yeah

earnest flume
#

Then you'll cross the enemy at all times if you try to get past

nocturne hornet
#

Ok, let me explain how to get that

next scarab
nocturne hornet
#

to define a mirror, you need a point and an axis

#

the point will act as the origin for the frame of reference for the mirror

#

it is important than for all computations you do you use this point as the origin (0, 0, 0)

#

then, you take the position p you want to reflect

#

and you project it unto the mirror (using Vector3.ProjectOnPlane)

#

let's call p' the projected p unto the mirror

#

and r the reflection

#

then you get r = p' - 2p

#

hm wait

#

no wait that's for reflecting around a point

#

no wait again, the correct formula is 2p' - p my bad 💀

#

like that

#

So

#

For each of your enemies, give them a mirror. In Unity a plane is defined as a point and it's normal, (to get the normal, just take your axis and use Vector3.Cross(axis, Vector3.up)

next scarab
#

Okay, to simplyfy you can use Vector3.Reflect, where the direction is player’s movement vector, and normal is enemy to player direction. The result would be enemy’s movement vector

nocturne hornet
#

then for each frame,

Vector3 playerPos = player.transform.position - PlaneOrigin
Vector3 projectedPos = Vector3.ProjectOnPlane(playerPos, PlaneNormal)
enemy.transform.position = projectedPos * 2 + playerPos + PlaneOrigin
nocturne hornet
#

With reflect it'd look like this

void Update()
{
  Vector3 playerPos = player.transform.position - PlaneOrigin
  enemy.transform.position = Vector3.Refelct(playerPos, PlaneNormal) + PlaneOrigin
}
#

With PlaneOrigin and PlaneNormal the two variables you set to define your mirror

earnest flume
#

Alright, thanks a lot! Really appreciate it!
I will get into implementing this on monday and will update this thread when I do

nocturne hornet
#

ok ok ^^ ping me because by then I'll have forgotten

earnest flume
#

Sure haha