Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by areFranz · Jul 31, 2014 at 05:22 PM · c#spherepipe

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 31, 2014 at 05:31 PM 0
Share

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.

avatar image areFranz · Aug 01, 2014 at 12:39 PM 0
Share

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?

avatar image robertbu · Aug 01, 2014 at 01:36 PM 0
Share

.. 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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges