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 Ppa0 · Oct 29, 2011 at 01:52 AM · c#colliderontriggerstayontrigger

C# OnTriggerStay Collider Problem

So I have a monster that has this script attached to it and I want it to move after it kills the cannon, but it just stands there:

 void Start()
 {
 speed = walkingSpeed
 }
 
 void Update()
 {
 transform.Translate(Vector3.right * speed * Time.smoothDeltaTime);
 }
 
 void OnTriggerStay(Collider col)
 {
 if (col.tag == "cannon")
 {
 attackingTimer += Time.deltaTime;
 speed = 0.0;
 if (attackingTimer > attacksEvery)
 {
 col.SendMessage("CannonTakesAHit", iDamage);
 attackingTimer = 0.0f;
 }
 }
 }

So basically hits the cannon over and over, but when the cannon dies, it stands there doing nothing, and I want it to keep moving forward.

I tried messing around with OnTriggerExit, speed = walkingSpeed with no success, I also tried adding else if col.tag != "cannon" it works but it takes random amount of seconds before it starts moving... which is not what I am looking for unless I can make it where it waits exact amount of time, every time, before moving. I also tried adding a send message in the cannon script where it sends hey this cannon is dead, keep moving and it works, but only for 1 monster, so if I have lets say 3 monsters attacking the same cannon, and they kill it only 1 of them starts walking and the rest stay behind.

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 GoSuNeem · Oct 29, 2011 at 07:39 AM

Technically, as far as I know, the cannon never left the trigger.

Few things you can do here. Have a "dead" layer where if a object(in this case a cannon) dies move it to the dead layer so the monsters can't collide with the cannon anymore. (Make sure you set up your physics collision correctly! ;])

Or, on the line "if (col.tag == "cannon")" add a check to see if the object is still alive.

Those are the most simple/quick fixes, but there's a lot of other ways to correct this that's a lot more efficient.

Another thing I would like to point out is that make sure you set your physics collision correctly... I would think this piece of code would run into few problems such as it's colliding with another monster first so it can't collide with anything else.

http://unity3d.com/support/documentation/Components/Layer%20Based%20Collision%20detection.html

Hope that helps! Thanks.

Comment
Add comment · Show 5 · 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 Ppa0 · Oct 29, 2011 at 02:16 PM 0
Share

Thanks, I am fairly new to Unity and I don't even know how to mess with layers properly so I rather not. I have been trying to figure out how to add a check to see if the object is still alive but unfortunately I don't know how to do that so I been trying different "jenky" things, but nothing has been working.

avatar image GoSuNeem · Oct 30, 2011 at 12:38 AM 0
Share

For changing the layer, all you have to do is get the gameObject then use .layer.

To set new layers, go to Edit>Project Settings>Tags. add in a new layer such as "$$anonymous$$onsters" "alive" and "dead". Remember that layer number! you'll need it later.

After, go to Edit>Project Settings>Physics.

Set it so monsters can't hit dead objects. and set your monsters and cannons to the correct layers (at the top right of your inspector)

when the cannon "dies" all you have to put is

gameObject.layer = *number of your dead layer object)

then you should all good to go.

avatar image GoSuNeem · Oct 30, 2011 at 12:40 AM 0
Share

The other way is to just set a bool on your cannon.

public bool alive = true; when it dies set the bool to false and in your monster code, add....

if (col.tag == "cannon") {

Cannon code name cannon = col.GetComponent<Cannon code name>(); if(cannon != null && cannon.alive) { attackingTimer += Time.deltaTime; speed = 0.0; if (attackingTimer > attacksEvery) { col.Send$$anonymous$$essage("CannonTakesAHit", iDamage); attackingTimer = 0.0f; } }

Sorry couldn't fit the rest in the last comment

avatar image Ppa0 · Oct 30, 2011 at 03:42 AM 0
Share

thank you very much, I appreciate your help

avatar image GoSuNeem · Oct 30, 2011 at 06:10 AM 0
Share

NP ;] I'm just trying to help out.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I'm having a problem with OnTriggerStay2D 1 Answer

Distribute terrain in zones 3 Answers

Work around for onTriggerStay? 1 Answer

Multiple Cars not working 1 Answer

Call function on all objects in trigger 2 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