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 evangwilliams · Sep 25, 2011 at 03:03 PM · collisionrigidbody

Collision detection occasionally fires too soon

I'm developing an infinite sidescroller, where the player has to avoid a variety of traps and survive as long as possible for a high score. The problem I'm having is with my collision detection- occasionally, a collision will be detected much too early, killing the player when they had clearly not touched a trap.

Here's the code governing the player movement (C#):

     void FixedUpdate () 
 {
     float x = speed * Time.deltaTime;
     Player.rigidbody.AddForce(0, FallSpeed * -1, 0);
     if (transform.position.y > 40)
     {
         Player.rigidbody.AddForce(0, FallSpeed * -10, 0);    
     }
     if (transform.position.x >= 1000)
     {
         float currentY = transform.position.y;
         float restartX = 0;
         transform.position = new Vector3(restartX, currentY, 0);
     }
     transform.Translate(x, 0, 0);
     if (Input.GetKeyDown(KeyCode.Space))
     {
         Player.rigidbody.Sleep();
         Player.rigidbody.AddForce(0, JumpForce, 0);
     }
     if (Input.GetKeyDown(KeyCode.X))
     {
         isCharging = 1;
         StartCoroutine(chargeTimer());
     }
     
 }
 IEnumerator chargeTimer()
 {
     float oldSpeed = speed;
     float oldFall = FallSpeed;
     transform.rigidbody.Sleep();
     FallSpeed = 0;
     speed = chargeSpeed;
     yield return new WaitForSeconds(chargeDuration);
     speed = oldSpeed;
     FallSpeed = oldFall;
     isCharging = 0;
 }

I use the sleeps to stop the forces currently acting on the rigid body before applying new ones, I wanted the jump to not observe gravity or current downward force applied to it.

And here's the collision detection code (currently attached to the trap objects in my scene)

     public GameObject Player;
 public GameObject Trap;
 public GameObject mainCamera;
 void OnCollisionEnter(Collision collision) 
 {
     ninjaScroll script;
     script = Player.GetComponent<ninjaScroll>();
     if (script.isCharging == 1)
     {
         Destroy(Trap);
     }
     else
     {
         mainCamera.transform.parent = null;
         Destroy(Player);
         StartCoroutine(MyLoadLevel(5, 0));
     }
     
     
     
 }
 IEnumerator MyLoadLevel(float delay, int level)
 {
     yield return new WaitForSeconds(delay);
     Application.LoadLevel(level);
 }
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 aldonaletto · Sep 25, 2011 at 06:12 PM

You're not checking if the object which collided with the trap is the player. If any other object hits the trap - even the ground - the Player will be destroyed. Try to verify this using the player tag or name. I would also set isTrigger in the trap's collider - triggers are easier to use if you don't need physics reactions (like the trap being thrown away by the player):

void OnTriggerEnter(Collider col){
    if (col.tag == "Player"){ // remember to set the player's tag to "Player"
        ninjaScroll script = Player.GetComponent<ninjaScroll>();
        if (script.isCharging == 1)
        {
            Destroy(Trap);
        }
        else
        {
            mainCamera.transform.parent = null;
            Destroy(Player);
            StartCoroutine(MyLoadLevel(5, 0));
        }
    }
}
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 evangwilliams · Sep 25, 2011 at 06:55 PM 0
Share

Thanks so much! That makes total sense. I've been writing c# for all of 3 weeks now so I'm still getting a lot of this sorted out. Totally resolved the issue :)

avatar image TailKitty · Mar 06, 2017 at 08:57 AM 0
Share

Thank you, Useful indeed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drag & Drop GameObject with MovePosition shoots other Gameobject away in case of them colliding 1 Answer

Collider going through walls with box collider.. 1 Answer

OnTriggerEnter not firing if rigidbody is on its side 0 Answers

Rigidbody and Parenting - Kinematic Rigidbodies? 2 Answers

collision without rigidbody 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