#Not sure if I knew the exact term but
1 messages · Page 1 of 1 (latest)
Fair enough
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
_oldPosition = transform.position;```
Would this be local frame of reference?
no, this is global. transform.position is always global
But using localPosition will give the same result if it's no childed, right?
Or am I thinking wrong
localposition gives you the frame of reference of the parent
Ahh
if there are no parent then yes it is global
Then I get that, just this is not clicking yet
Sorry, been working for most of the day already, so not as sharp as in the morning 😅
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 ?
Ah, yeah
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 ?
I think so yeah
MovementPerFrame = transform.InverseTransformVector(transform.position - _oldPosition);
_oldPosition = transform.position;
This seems to give the right value for the player movement
Yes, it would give you the player's movement in the local frame of the player
_trackers.position += transform.TransformVector(_playerInfo.MovementPerFrame);
This mirrored correctly, so now I just gotta flip x
Sure, thanks a bunch!
Think I'll get it working now!
👍
And learned some more linear algebra for games haha
linear algebra is the single most useful theory of maths when it comes to game dev
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
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
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;
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 :) )
What exacyly are you trying to do?
Have an enemy behave like you are looking in a mirror basically
There is many ways of “Mirroring”. So you want axial symmetric movement?
Yes
And what would be the axis?
But there is no 1 axis, since the player can freely move. The enemy will procedurally spawn
So you want your movement vector symmetric, not position.
What do you mean with that?
Sorry had math in dutch all my life so the terms are sometimes a bit fuzzy
I will come back and explain soon
Sure!
Okay is this what you want?
Where the blue is player and red is enemy
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
re
you can rewrite what is considered forward with a quaternion
so then if the player walks toward the enemy, it is counted as "forward"
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
actually this mirror idea is not bad
you can simulate a "mirror" by reflecting the position either around an axis or a point
https://forum.unity.com/threads/calculate-mirror-position-over-tilted-axis.1218840/
Not sure how useful this would be, but might help
ok, so how to reflect a position around an axis
Okay, so is this what you want?
Oh yeah
Then you'll cross the enemy at all times if you try to get past
Ok, let me explain how to get that
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)
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
then for each frame,
Vector3 playerPos = player.transform.position - PlaneOrigin
Vector3 projectedPos = Vector3.ProjectOnPlane(playerPos, PlaneNormal)
enemy.transform.position = projectedPos * 2 + playerPos + PlaneOrigin
ooooh I didn't know Unity had a reflect function, that's cool
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
Alright, thanks a lot! Really appreciate it!
I will get into implementing this on monday and will update this thread when I do
ok ok ^^ ping me because by then I'll have forgotten
Sure haha