- Home /
Locking the Z-axis based on direction
I'm having a bit of trouble figuring out the maths for this one.
I have a series of nodes, each one has a position and direction, the player is always linked to one of these nodes, and can be anywhere on the line it describes (x direction) or above or below it (y direction), but cannot move into the foreground/background (z direction) relative to the direction the node is pointing I've got the moving in 1 axis working, but I need to snap the player to the relative Z of the object (no matter where they are on the line) Movement is done with the following code:
void Update()
{
float Direction = Input.GetAxis("Horizontal");
this.transform.position += (CurrentNode.transform.rotation * Vector3.forward) * Direction * Time.deltaTime * Speed;
}
this is not as simple as setting the z = 0, as that only works if the player is facing left/right relative to the game space.
I've thought about using 2 rays and finding their intersection point (Node ray is node position and node rotation, Player ray is Player Position and Player rotation Vector3.Left**) but afaik the Ray class hasn't got an intersecting function.
Vector Math has never been my strong point sadly and I'd appreciate any help.