- Home /
How to move a ship inside a pipeline
Hi All! I'm developing a pipeline racing game in which a ship moves inside a pipe. I need that the movement is smooth and the ship os always touching the internal surface of the pipeline, and it rotates by side along the pipe surface. Now i have a scene setted in this way:
1) The Pipelin (flipped direction) width meshcollider and rigidbody kinematik without gravity with high mass
2) The Player, that is a empty GO with sphere collider that has the diameter of 1 - the diameter of the pipe, and a rigidbody non kinematik and non gravity with the following script
3) Childs of the Player, there are the ship and the camera.
The ship is positioned at the bottom of the spehere near the pipe surface, makeing the sensation of touching it
All that makes life with this C# script that controls the Player (the Sphere with attached the ship):
using UnityEngine;
using System.Collections;
public class PipeSphereMotion : MonoBehaviour {
public float speed;
public float torqueSpeed;
private Vector3 dir;
void FixedUpdate () {
RaycastHit leftHit;
RaycastHit topHit;
RaycastHit rightHit;
RaycastHit downHit;
Physics.Raycast (transform.position, Vector3.left, out leftHit);
Physics.Raycast (transform.position, Vector3.up, out topHit);
Physics.Raycast (transform.position, Vector3.right, out rightHit);
Physics.Raycast (transform.position, Vector3.down, out downHit);
dir = (Vector3.Cross (leftHit.normal, topHit.normal) + Vector3.Cross (rightHit.normal, downHit.normal)).normalized;
if (Input.GetAxis ("Vertical") > 0)
rigidbody.AddRelativeForce (-dir * speed);
if (Input.GetAxis ("Vertical") < 0)
rigidbody.AddRelativeForce (dir * speed);
if (Input.GetAxis ("Horizontal") > 0)
rigidbody.AddRelativeTorque (-dir * torqueSpeed);
if (Input.GetAxis ("Horizontal") < 0)
rigidbody.AddRelativeTorque (dir * torqueSpeed);
}
}
Now, in the rectilinear it's all ok: the ship runs forward and strafes along the sides of the pipe, but when arrives the curve, the sphere inside the pipe rolls around... Is there a method (maybe with better raycasts, i think i do the mine wrong) to force the sphere to face always forward to the pipe irection?
If it were me, I'd simulate the movement ins$$anonymous$$d of using Unity Physics (or in conjunction with Unity Physics). I'd create a spline down the center of the pipe and move the object along the spline. You can do a spline with iTween (free in the asset store) and iTween.PointOnPath() and/or iTween.PutOnPath(). Or there are other spline packages in the Asset Store. Or spline code has been posted on UA.
I've already tried many approaches, the total scripting (raycasts that push the ship to the surface of the pipe and the pipe with a phisics material set to ice, but the ship don't turn well)... But i think that with a spline the ship don't touch the surface of the pipe but it moves in the center of it along the spline...am i wrong?
.. But i think that with a spline the ship don't touch the surface of the pipe but it moves in the center of it along the spline...am i wrong?
II don't know the mechanics of your game or your pipe (which is why I made my suggestion as a comment rather than an answer), but you can move an empty game object along the spline and allow the ship as a child object to deviate from the empty game object by a set radius (or based on the shape of the ship). This keeps the ship inside the pipe, moving smoothly forward, but allow the ship to move within the pipe.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Change and Access another scripts Variables 1 Answer
Javascript version of 'Mouse look' script? 1 Answer
looking to make for a zombie script AI,navmesh or not? 2 Answers