Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AmirMahmood · Jan 28, 2016 at 05:43 PM · 2d-platformerground detectioncircularcolission

Check if circle player is on the ground

hi
How can I check if a circle is on the ground like picture below ?!
i have roof and wall in my game , so if i check ground collision for all around of circle , it can jump sevral times on walls or roof.

sorry for my bad english. alt text

ground.png (20.5 kB)
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

4 Replies

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

Answer by VivienS · Jan 28, 2016 at 08:06 PM

Hey there,

If you're using simple colliders and

 void OnCollision2D (Collision2D collision) {...}

or its 3D counterpart, check out the Collision2D.contacts. It's an array that contains the exact positions of the collision points in world space. You can transform them easily into local space or just compare them with the world position of your collider (below => probably hit floor, same => probably hit wall, ...).

If you're using a CharacterController, you can check its CollisionFlags to see where the last hit took place.

Cheers.

Comment
Add comment · Show 1 · 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 AmirMahmood · Jan 29, 2016 at 10:36 AM 0
Share

it works like a charm ! thank you VivienS .

avatar image
1

Answer by coolraiman · Jan 28, 2016 at 07:46 PM

Use box collider (boxCollider2D if your game is in 2D) and give them the name "ground" and align their position and size with your ground surfaces

add a rigibody on your circle and add a script on your circle with something like this exemple

 public class ExampleClass : MonoBehaviour {
     void OnTriggerEnter(Collider other) {
         if(collider.name == "ground")
         {
         //do your stuff here
         }
     }
 }
Comment
Add comment · 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
0

Answer by wolfenstien98 · Jan 29, 2016 at 06:36 AM

If you are using a Character Controller you can use the built in method isGrounded.

Otherwise I would suggest using a RayCast

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

Answer by Fogwalker · Jan 31, 2016 at 02:38 PM

I think I know you problem... using charactercontroller might not work in your situation although it might.

If you are doing this 2d.. watch this video.

https://www.youtube.com/watch?v=2akPDnmSfu8

If you are doing this in 3d watch the video, but the code will be close to this.

using UnityEngine; using System;

public class jump : MonoBehaviour { // These are the variables for detecting ground

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;
 private bool grounded;


 // This is for the jump variables
 public float thrust;
 public Rigidbody rb;

 // Use this for initialization
 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update()
 {
     grounded = (Physics.CheckSphere(groundCheck.position, groundCheckRadius, whatIsGround));

     if (Input.GetKeyDown("space") && grounded)
     {
         rb.AddForce(Vector3.up * thrust);
     }

 }

}

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

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

6 People are following this question.

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

Related Questions

No response from raycast 0 Answers

[2D] Check if player is on the ground 4 Answers

Jump if ground is detected 1 Answer

Player falls through ground after adjusting pixels per unit of the players sprite 0 Answers

How do I set Ground Check to a size-changing character? 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