- Home /
Finding the axis of a curving pipe
I am making a game similar F-Zero or Wipeout, where at times the player travels over the surface of a pipe, so the movement should always be relative to the pipe.
My prototype has been using the built in Cylinder primitive as the pipe, so I can get the long axis of the pipe by finding the Up vector of the cylinder the player is above. However, the goal is to have curved pipes, so I won't just be able to use the pipe's up vector to find the 'axis' of the pipe, which is curved. Does anyone have experience with this sort of issue?
I essentially need a better way of finding 'newforward' in the following code.
RaycastHit hit;
Physics.Raycast(controller.position, -controller.transform.up, out hit);
Vector3 gravityDirection = hit.normal;
Vector3 newforward = hit.collider.transform.up;
Vector3 perp = Vector3.Cross(gravDirection, newforward).normalized;
moveDirection = (perp * Input.GetAxisRaw("Horizontal")) * speed;
moveDirection+= Input.GetAxisRaw("Vertical") * newforward * speed;
I offer a couple of tentative suggestions. For an arbitrarily shaped pipe, you could attach a spline. There are a variety of tools for building splines in Unity, or you could approximate it with a path of points. If the goal is to figure out if a character should fall off the pipe or not, you could use the RaycastHit.normal and compare it with some universal up.