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 dBlue · Dec 26, 2015 at 10:46 PM · collision2dif-elsecontinuous

Collision checked once, then never again. How can I make it always check if its touching? C#

I have a function that checks for collisions, I was hoping I could have it change the animation when it sees that I'm not touching or am touching an object. The problem is that it only seems to check the instant I hit play and never again, so I can't change the animation. Is there a way to fix that? The function that is the problem is the last one.


using UnityEngine; using System.Collections;

 public class Player : MonoBehaviour {
 public float speed = 1f;
 public float jumpSpeed;
 public Rigidbody2D rb;
 Animator anim;
 
 void Start () {
     anim = GetComponent<Animator>();
     rb = GetComponent<Rigidbody2D>();
 }
 
 void Update () {
     if (Input.GetKey (KeyCode.D) && (transform.position.x < 3.5)) {
         transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
         transform.localScale = new Vector3 (2.25f, 2.25f, 0f);
     }
     
     if (Input.GetKey (KeyCode.A) && (transform.position.x > -3.5)) {
         transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0f);
         transform.localScale = new Vector3 (-2.25f, 2.25f, 0f);
     }
     
     if (Input.GetKeyDown (KeyCode.S)) {
         gameObject.GetComponent<Rigidbody2D> ().gravityScale += 1;
     }
     
     if (Input.GetKeyDown (KeyCode.W)) {
         gameObject.GetComponent<Rigidbody2D> ().gravityScale -= 1;
     }

     if (Input.GetKeyDown (KeyCode.Space)) {
         rb.AddForce(transform.up * jumpSpeed);
     }
 }

 void OnCollisionEnter2D(Collision2D col) {
     if (col.gameObject.name == "Platform01") {
         gameObject.GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezePosition;
         anim.SetBool ("istouch", true);
         Debug.Log ("True");
     } else {
         anim.SetBool("istouch", false);
         Debug.Log("False");
     }
 }

}

Comment
Add comment · Show 2
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 PabloUnityArgentina · Dec 26, 2015 at 11:53 PM 0
Share

The object that is hitting the "Platform01" has to exit the collision area of the Platform01 and enter again in order to trigger again the event OnCollisionEnter2D.

Are you sure you are using a rigidbody2d component and Collider2d components in both gameobjects (this game object and the "platform01").

Is your rigidbody2d platform kinematic? If not, consider to make it kinematic. $$anonymous$$ark your Platform01 GameObject as static, and put gravityscale in 0 value. From the editor, not from code, freeze all constraints, and don't use rigidbody functions affecting Platform01.

But...

Why are you freezing the rigidbody2d of the platform01 inside OnCollisionEnter2D?

avatar image dBlue PabloUnityArgentina · Dec 27, 2015 at 12:43 AM 0
Share

I want to freeze the position on the platform to prevent lots of shaky movement to the player while the platform moves. It works, the debug only ever outputs "false," I assume because it only checks once and the player starts off the platform. I'm not using $$anonymous$$inematic but even if I do check it on the platform it doesn't seem to make a difference anywhere. Both the platform and the player have rigid bodies and colliders.

3 Replies

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

Answer by Measurity · Dec 27, 2015 at 12:59 AM

I think you want to change OnCollisionEnter2D to OnCollisionStay2D:

Or set the anim.SetBool("istouch") to true in:
OnCollisionEnter2D and false in: OnCollisionExit2D

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
1

Answer by Kiwasi · Dec 27, 2015 at 12:50 AM

Sounds like you want OnCollisionStay instead of OnCollisionEnter. Be warned, lots of OnTriggerStay calls will kill performance.

The other option is to track objects in OnColissionEnter and OnCollisionExit. Use a list or a bool to keep track of the state and respond appropriately.

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 PabloUnityArgentina · Dec 27, 2015 at 12:59 AM

The object that is touching the "Platform01" has to exit the "collider zone" and enter again in order to trigger the OnCollisionEnter2D function again.

Are you sure you have rigidbodies2D an Colliders2D for those two game objects?

Is the rigidbody from Platform1 Kinematic?

Why are you freezing the position of that rigidbody inside the OnCollisionEnter function?

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

33 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

Related Questions

Segment within a nested If does not get evaluated 2 Answers

I want my vehicle to keep moving left/right while the buttons are held down? 1 Answer

Continuous number scroll picker UI using image and Lerp? 0 Answers

OnCollision2D will not work for me 1 Answer

Two Different Box Colliders Aren't Colliding 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