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
1
Question by Cowtat0 · Oct 23, 2017 at 04:05 AM · collision detection

I have no idea how to check if two 2D objects are colliding

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ballMovement : MonoBehaviour {

 public GameObject bowlingBallObj;
 public GameObject foregroundObj;

 public float leftForce = -50f;
 public float rightForce = 50f;
 public float upForce = 2000f;

 private bool isDead = false;
 private Rigidbody2D rb2d;

 

 CircleCollider2D bowlingBall = bowlingBallObj;
 BoxCollider2D ground = foregroundObj;


 // Use this for initialization
 void Start () {
     rb2d = GetComponent<Rigidbody2D>();
     bowlingBallObj = GameObject.Find("Bowling Ball");
     foregroundObj = GameObject.Find("Foreground");
 }
 
 // Update is called once per frame
 void Update () {
     
     if (isDead == false)
     {
         if (Input.GetKey(KeyCode.A))
         {
             rb2d.AddForce(new Vector2(leftForce, 0));
         }

         if (Input.GetKey(KeyCode.D))
         {
             rb2d.AddForce(new Vector2(rightForce, 0));
         }

         if(bowlingBall.IsTouching(ground))
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 rb2d.AddForce(new Vector2(0, upForce));
             }
         }
     }

 }

} I am just trying to make a simple program where a bowling ball can roll and jump around once. I want it so the bowling ball can't jump twice and can only jump while touching the ground.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by adipatil3517 · Oct 23, 2017 at 04:47 AM

write function OnCollisionEnter2D(collider other) { Debug.Log("Colliding"); } if two objects are colliding you will get to know about it

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 Tony_2015 · Oct 23, 2017 at 06:22 AM

A better way of doing it would be using Physics.checkSphere. Hope this helps.


//Make bool like say Grounded Public bool Grounded;

//this is the position where the object will check for ground

Public GameObject GroundChecker;

//this is your ground. Put your ground object on a new layer called ground and then select the ground layer in the script

Public LayerMask Ground;

Void Update() {

Grounded = Physics.CheckSphere(GroundChecker.transform.position,0.05f,Ground);

If(Grounded) { (Input.GetKeyDown(KeyCode.Space)) { rb2d.AddForce(new Vector2(0, upForce)); } } }

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

72 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

Related Questions

How to make particles collide with something but at the same time phase through it 2 Answers

How do I fix this weird collision "bug"? 1 Answer

use OnParticleCollision(GameObject)for AOE spell/magic creation problem 1 Answer

How to detect different parts in sprite? 2 Answers

Why is the material switching back after collision? 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