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 drodrii · May 20, 2014 at 03:27 PM · collisioncollidertransformsprite

Stop moving when colliding. 2D

Hello everyone, hope you're doing awesome!

So, I'll make this quick.

Im moving a sprite using this script.

moveSprite.cs (Attached to moving object).

 if(buttonIsPushed)
 {
 
             
             transform.Translate (Vector3.left * speed * Time.deltaTime);
 
             //Clamping user from movign outside map bounds.
             tempPosition = transform.position;
             tempPosition.y = Mathf.Clamp(tempPosition.y, minY, maxY);
             tempPosition.x = Mathf.Clamp(tempPosition.x, minX, maxX);
             transform.position = tempPosition;
 
             
             //Button false
             gui.leftClicked = false;
             
 }
 
 

Quick scan.

1.Transform moves sprite, 2.Map clamp so user doesn't go outside map bounds.

Now I want to restrict the sprite from moving if he's colliding with another gameObject.

How could I achieve this?

Thanks for your tips & help!

Have an awesome day!

:D

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 robertbu · May 20, 2014 at 03:45 PM

You can use OnCollisionEnter2D() and OnCollisionExit2D() to set a flag line 'isColliding'. Then change line 1 to:

 if (buttonIsPushed && !isColliding)

Alternately, if your sprite is a square or a circle (or you can live with a square or circle detection), you can use Physics.OverlapCircle() or Physics.OverlapArea() to detect if your object is colliding.

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 drodrii · May 20, 2014 at 06:17 PM 0
Share

@robertbu

Hey! I've added this to the same script:

 void OnCollisionEnter2D (Collision2D other) {
 
         Debug.LogWarning ("TriggerEnter");
         Debug.Log (collider.gameObject.name);
         Debug.Log (other.gameObject.name);
         canWalk = false;
             
     }
 
     void OnCollisionExit2D (Collision2D other) {
 
         Debug.LogWarning ("TriggerExit");
         canWalk = true;
         }


Added boxcollider 2d component to the player and obstacle..but neither is being called...

avatar image robertbu · May 20, 2014 at 11:00 PM 0
Share

Do you have a non-kinematic Rigidbody2D on either object? You use the word 'Trigger' in your code. If you have the isTrigger flag set, you want to use OnTriggerEnter2D() and OnTriggerExit2D().

avatar image drodrii · May 22, 2014 at 01:22 AM 0
Share

@robertbu sorry for late repliance, and thanks for your time.

I added BoxCollider2D component to the player, the obstacle and clicked 'isTrigger'

Then changed the function to OnTriggerEnter2D() { Debug.Log("Enter")}

Still nothing.

Where can I see if there's something like non-kinematic. As far as Im concerned I haven't added any component of the likes. Just boxCollider, transform and sprite renderer.

Is it possible that its because they're on different sorting layers on the sprite renderer?


All this is inside the moving script. Should I create a script and add it to both? or should it run either way?

avatar image drodrii · May 22, 2014 at 03:36 AM 0
Share

@robertbu

Ok, so after I sent my message I forgot to add RigidBody, I added it. Now my question is. From the original code, I added the same with OnTrigger, why is OnCollisionEnter not being called? Plus another question to your solution.

When player touches object, 'isColliding' is true. So I can't set isColliding to false because player can't move.(Due to it colliding with object). What would you do to get over this problem...?

Thanks in advance, you always have time for us dumdums haha, cheers! :D

avatar image Noob_Vulcan · May 22, 2014 at 04:44 AM 0
Share

@$$anonymous$$ii hey can tell me in brief what scenario you are trying to achieve. I would love to help. Because from above conversations i cant get clear idea abt the scenario.

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Ignore Collision Temporarily 1 Answer

2D Sprite Collision with 3D Object 1 Answer

Collision detection not working properly with 2D sprites 1 Answer

Multiple Colliders With Mechanim 1 Answer

How to check if sprites are overlapping and by how much? 0 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