• 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 SBull · May 24, 2013 at 06:44 PM · 2dcollisionmovementrigidbodydetection

How can I make a non-player character (rigidbody) move when they touch the ground?

Sorry if something similar is explained in another question already, but I have been searching for a while and have not been able to find an answer. I'm very new to Unity and scripting so please bear with my ignorance.

I'm making a 2D game where the player does not control a character directly, but guides multiple characters by interacting with the environment. Therefore I need the characters to move on their own. For starters, I would just like them to start moving to the right as soon as they hit the ground, but am not sure what the best way to do this is. I can't seem to find code to tell an object to constantly move in one direction or how to tell if two rigidbodies (the character and the ground) are colliding. Would it be better to use several CharacterControllers or am I better off keeping them all as rigidbodies? Any help is appreciated.

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
1
Best Answer

Answer by MikeNewall · May 24, 2013 at 07:15 PM

Cast a ray at the floor to see if there is a collider below your character and if there is then add a force to your character.

 public float moveForce = 0.5f;
 private float characterHeight = 0;
 
 void Start()
 {
     // Cache the characters height
     characterHeight = renderer.bounds.size.y;
 }

 
 void FixedUpdate()
 {
     if(isGrounded())
     {
         rigidbody.AddForce(Vector3.right * moveForce);
     }
 }
 
 bool isGrounded()
 {
     if(Physics.Raycast(transform.position, Vector3.down, characterHeight /2))
     {
         Debug.Log("Grounded");
         return true;
     }
     else return false;
 }
Comment
Add comment · Show 2 · 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 SBull · Jun 06, 2013 at 05:18 PM 0
Share

Thank you! That works.

avatar image Fattie · Jun 06, 2013 at 05:43 PM 0
Share

please Tick an answer, to close out the question. thanks. Round button with Tick.

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

15 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

Related Questions

Make characters go through each other 1 Answer

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 Answers

Player glitching through wall when sprinting 1 Answer

2D Collision 1 Answer

Player-movable rigidbodies w/ collision? 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