• 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 Fuzzy_Fuzzy · May 06, 2018 at 01:33 PM · physicsrigidbodyraycastcubeball

Make a rolling ball always on ground without falling when reaching the edges of the map

Greetings everyone and a happy mother's day to the mothers in this community d(^^)
So i'm working on a 3D project that involves a ball rolling around a cube. The player presses a key and it activates a kinda of magnet to the ground, that means the player gets stick to the ground and continue to follow the ground without falling to infinity and beyond because of the gravity, the ball has a rigidbody and the cube only has a box collider.
When the player presses ,i.e T, there'll be a bool that'll store if the player has presses the key and if so, then the player is in stick mode, let's call it that for kicks and giggles.

Now my question is, how to I achieve this effect? I've been researching and I saw people using Raycast as a solution, but I'm not able to achieve what I need.

Here's my move player code

 private void Update()
     {
         MovePlayer();
         
     }
 
 
     void MovePlayer()
     {
         float moveHorizontal;
         float moveVertical;
 
         if (!invertedCamera)
         {
             moveHorizontal = Input.GetAxis("Horizontal") * invertX;
             moveVertical = Input.GetAxis("Vertical") * invertZ;
         }
         else
         {
             moveHorizontal = Input.GetAxis("Vertical") * invertZ;
             moveVertical = Input.GetAxis("Horizontal") * invertX;
         }
 
 
         Vector3 movePlayer = new Vector3(moveHorizontal, 0.0f, moveVertical);
         playerRB.AddForce(movePlayer * movementSpeed);
 
 
         check_if_stick(); //I don't Know How To program this part =(

         change_camera_view();
     }

 void check_if_stick()
     {
         if(Input.GetKeyDown(KeyCode.T) && !keyDown)
         {
             if (!isStick)
             {
                 isStick = true;
                 //What should i do here?
             }
             keyDown = true;
         }
         if (Input.GetKeyUp(KeyCode.T) && keyDown)
         {
             keyDown = false;
         }
     }

Now my problem is, how to create the RayCast, also is this the best way to achieve what I want? Or is there another way? Should i create this check_if_stick on a new c# script file?

Thank you so much for your time and help, i'm still a little noobish but getting there =) Best reguards =)

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

Answer by anthot4 · May 08, 2018 at 11:44 AM

Is the ground flat the whole time and not moving? There are a few ways you could do this.

Comment
Add comment · Show 6 · 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 Fuzzy_Fuzzy · May 08, 2018 at 12:02 PM 0
Share

greetings anthot4, thank you for your reply it's like a cobble so a cube with, let's say, 20 width , 20 length and 1 height

avatar image anthot4 Fuzzy_Fuzzy · May 08, 2018 at 12:12 PM 0
Share

I think the easiest way to do with would be to add a downwards rigid body force. I'm not sure how you would do this by raycasting. It would be best to put the CheckIfStick() function in your current script.

avatar image Fuzzy_Fuzzy anthot4 · May 08, 2018 at 12:15 PM 0
Share

downwards rigid body force?
Would you $$anonymous$$d explaining please? =)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

183 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player-controlled rigidbody ball sometimes slowing down for no reason 0 Answers

How do I make a cube rigidbody pushable by the default 3rd person character controller without tilting and going left and right? 0 Answers

Predicting ball direction on collision 0 Answers

airplane collision detection problem 1 Answer

Moving a ball around a maze 3 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