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 kevinspawner · Aug 11, 2014 at 09:24 AM · collisionmovementplayercontroller

Moving a Simple Ball [Main Player!] inside a tunnel without gravity!

Am planning to build a game which involves making a simple Ball as a main character and it will move inside a Tunnel. The player can Move forward, Left and Right. Also the player can basically go 360 degree inside the tunnel. So, it will not have a gravity system.

Collision Detection need to be High as the game move forwards the speed increases. The interested thing about this game is it will be fast and player can run through 360 degree in the tunnel.

I would highly appreciate for your valuable input and time. Thanks in advance.

P.S: I use Unity c#

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Scribe · Aug 11, 2014 at 01:41 PM

Hey there, are you sure you actually need physics to do this? Often for this type of thing you would keep the ball in the same z position and move or create a tunnel around it.

I have set up a simplish script which you can see below, which will work given that you can find the correct 'centre' position, however if you are moving the ball, especially in a changing direction (a curved tunnel) finding the centre from which to apply gravity becomes more difficult. In that case you will have to code a slightly more complicated and more expensive (computationally) script where you find the normal of the collision between the ball and tunnel surface and apply gravity in the opposite direction of that. You can get the normals of the contact points from OnCollisionStay

Here is a more trivial approach for simplified cases!

 float gMag = 9.8f;
 float moveForce = 5f;
 Vector3 centre = Vector3.zero;
 private Vector2 gDir = Vector3.zero;
 private Vector2 gPer = Vector3.zero;
 
 void FixedUpdate () {
     gDir = transform.position == centre ? -Vector3.up : (transform.position - centre).normalized;
     gPer = new Vector2(gDir.y, -gDir.x);
     
     rigidbody.AddForce(gDir*gMag, ForceMode.Acceleration);
     if(Input.GetKey(KeyCode.RightArrow)){
         rigidbody.AddForce(-gPer*moveForce);
     }else if(Input.GetKey(KeyCode.LeftArrow)){
         rigidbody.AddForce(gPer*moveForce);
     }
 }

Hope that gets you started!

Scribe

Comment
Add comment · Show 5 · Share
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 kevinspawner · Aug 13, 2014 at 02:55 AM 0
Share

Thanks for your quickly reply. Highly appreciated. I will check this code in a simple cube and will post the findings. Also, is it needed to have code for collision detection separately for the tunnel?

avatar image Scribe · Aug 14, 2014 at 06:28 PM 0
Share

I'm a bit confused by your question, this should work as long as your tunnel has a collider and your object has a rigidbody!?

avatar image kevinspawner · Aug 17, 2014 at 11:28 AM 0
Share

Hi there, sorry for the delayed reply. I did tried this script and the ball moves and spins everywhere on the surface. I Have added the tunnel mesh with collider. All I need is just the ball need to move inside the tunnel 360 degree. But now it moves everywhere without any proper control

avatar image kevinspawner · Aug 17, 2014 at 11:31 AM 0
Share

Yes, object has a rigid body. I guess as per your advice this method will be more expensive in terms of memory consumption. However, is it possible to move just the tunnel? If so how to make collision happen? $$anonymous$$eaning the Tunnel mesh is curvy..

avatar image Scribe · Aug 18, 2014 at 10:23 AM 0
Share

So in my setup the tunnel is not the actual collider, I have an invisible ring of box colliders ins$$anonymous$$d, as the inside of a tunnel is a concave shape, and generally collisions with concave colliders are either unreliable or expensive.

collider screenshot

Then my ball has its rigidbody which will only ever collide with this set of box colliders, then the tunnel is not given a collider at all.

You can have a go on my quick 'n' dirty setup here if you want (A/leftArrow and D/rightArrow move the ball).

You can also download the package if you wish to take a closer look at some parts of it, the procedural tunnel generation might help as well!

Download Link

Hope that helps you out,

Scribe

screenshot 2014-08-18 11.13.24.png (239.2 kB)

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to hold objects in third person? 1 Answer

All Unity Engine games' controls not working 1 Answer

how do i move my player left and right using touch pad? 1 Answer

Issues Making Character Move With Rigidbody 1 Answer


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