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
2
Question by TaylorAnderson · Dec 02, 2013 at 02:13 AM · ontriggerstay

OnTriggerStay2D working only when player ISN'T moving

I've looked for this but I've only seen this question asked in the opposite, namely that OnTriggerStay or OnTriggerEnter is only working when their player IS moving. For my case, for some weird reason, my OnTriggerStay2D only works when my player ISN'T moving.

I'm trying to do knockback when the enemy hits a certain collider thats a little bit ahead of the player and the player is attacking. Both the enemy and the box collider ahead of the player are set to isTrigger. Here's the code in the enemy when he collides:

 void OnTriggerStay2D(Collider2D c)
 {
     if (c.tag == "Player")
     {
         Player player = (Player)c.transform.parent.GetComponent<Player>();

         if (player.attacking)
         {
             Debug.Log ("Anybody home?");
             myRigidbody.AddForce(transform.InverseTransformDirection(c.transform.position - transform.position) * -500);
         }
     }

 }

This code works fine for me in terms of knocking back the enemy, but it only works when the player is standing still.

Any help with this would be greatly appreciated!

Comment
Add comment · Show 6
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 TaylorAnderson · Dec 02, 2013 at 03:40 AM 0
Share

I'm bumping this because I seriously can't figure this out. Nothing I can find should make this happen.

avatar image TaylorAnderson · Dec 02, 2013 at 05:59 AM 0
Share

I've removed all if statements within the function so theres nothing else that could be causing it. Just straight up, OnTriggerStay won't work unless I'm standing still.

avatar image Jeffom · Dec 02, 2013 at 12:58 PM 0
Share

Can you debug and check if the colliders are really touching or that player collider is within the trigger? Also it could be a bug from the new 2D features.

avatar image TaylorAnderson · Dec 02, 2013 at 04:12 PM 0
Share

I've checked both: the enemy is definitely hitting the trigger, and I just moved the trigger outside the player's hitbox and its still not firing

avatar image Jeffom · Dec 02, 2013 at 07:30 PM 0
Share

Does your trigger have a rigid body? And for some trivial debug, have you set a Debug.log within the body of the OnTriggerStay just to check if its firing?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by belvita · Dec 03, 2013 at 01:32 PM

use void OnTriggerStay only

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 TaylorAnderson · Dec 03, 2013 at 04:57 PM 0
Share

I'm working with 2D stuff and that function doesnt really work with 2D stuff. I've decided I'm just scrapping that method altogether and using raycasts. They're working more reliably already.

avatar image
0

Answer by GamersFrenzy · Jul 15, 2014 at 04:10 AM

Not the script, add Rigidbody to both the Player and the Trigger Collider and check kinematic on both I also set Collision Detection to continuous That should help out.

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

19 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

Related Questions

OnTriggerEnter only working once. 0 Answers

Detect gameObjects within range and assign them as movetarget 1 Answer

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

Change cursor on OnTrigger 0 Answers

Work around for onTriggerStay? 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