#Vector math refresher

1 messages · Page 1 of 1 (latest)

edgy wadi
#

Trying to calculate the position of an offset based on a direction, this picture summarizes what Im thinking of bet but its been a while so Ive been stuck on this, in the 3d space I have an idea how to do this but no clue on 2d lol

#

Assume the bottom black dot is 0, 0, and the direction is 0.7, 0.7 lets say. Assuming some object or player is facing that direction, I want to get the local offset from that direction, lets say (1, 0) (dotted line indicates where it roughly should be)

#

Been a while since Ive done vector math like this so would appreciate a refresher!

blazing cypress
#

So, using a local direction you want a local offset?

river remnant
#

cross(direction, planeNormal) gives you the orthogonal normal of the direction in the plane. 2D is just 3D with the planeNormal being (0,0,1)

shell onyx
#

Vector2.Perpendicular does this

#

Or (x ,y) -> (-y, x)

#

If I even understand the issue correctly

uneven lichen
edgy wadi
#

Probably shouldve made it a bit more clear in the diagram, but my question was to add the offset based on the local direction

So in the diagram I made earlier, (-1, 0) from the direction dot would be 1 unit to the left, but because of the directional vector it gets shifted

#

But I did end up solving this with a different approach, Ill share it here if I get around to it later

uneven lichen
#

or if you don't want it affected by the object's scale:

Vector3 localOffset = new(-1, 0);
Vector3 rotatedOffset = transform.rotation * localOffset;
Vector3 finalPoint = transform.position + rotatedOffset;```
edgy wadi
#
Vector2 spawnDirection = projectileSpawnRule.SpawnLocalOffset.normalized;
Vector2 right = new Vector2(lookDirection.y, -lookDirection.x);
Vector3 spawnPos = (Vector2)transform.position + lookDirection * 
spawnDirection.y + right * spawnDirection.x;
#

Something like this is what worked for me

#

slight limitation because I didnt have access to transform.TransformPoint since I wanted to get the position for another object