Quantcast
Channel: Questions in topic: "pipe"
Viewing all articles
Browse latest Browse all 89

How to move a ship inside a pipeline

$
0
0
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?

Viewing all articles
Browse latest Browse all 89

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>