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 Saravia95 · Apr 29, 2014 at 10:24 PM · errortagsboxcolliderignorecollision

Underwater Collision Problem

I have an underwater slow effect in place whenever my character goes under water.

Here is the code for that:`using UnityEngine; using System.Collections;

public class WaterPhysics : MonoBehaviour { //CapsuleCollider UnderWater;

     // Use this for initialization

     Animation _animation;

     void Start ()
     {
     
             _animation = GetComponentInChildren<Animation> ();
     
     
             //UnderWater = GetComponent<CapsuleCollider> ();
     }

     private void OnTriggerStay (Collider Submerge)
     {

             GameObject thePlayer = GameObject.FindGameObjectWithTag ("Player");
             PlayerMovement PlayerMovement1 = thePlayer.GetComponent<PlayerMovement> ();
 
         


             if (Submerge.tag != "Water1") {
         

     
                 
                     _animation ["RunForward"].speed = 1;
                     _animation ["RunBackward"].speed = 1;
                     _animation ["RunRight"].speed = 1;
                     _animation ["RunLeft"].speed = 1;
             
                     PlayerMovement1._gravity = 2;
                     PlayerMovement1._moveSpeed = 25;
             
                     
             } else if (Submerge.tag == "Water1") {

             
                     //Debug.Log ("we Are underwater!");
                     _animation ["RunForward"].speed = 0.5f;
                     _animation ["RunBackward"].speed = 0.5f;
                     _animation ["RunRight"].speed = 0.5f;
                     _animation ["RunLeft"].speed = 0.5f;

                     PlayerMovement1._gravity = 1.0f;
                     PlayerMovement1._moveSpeed = 5.0f;
                     
     
             }
     }

} `

It works properly until I my Raptor game object which follows me, gets close. It has it's own boxcollider that I have set so it can switch animations from walk to sprint once I get close enough to it.

The problem is, while under water, I think that once the raptor gets in range, the script picks up his tag or boxcollider rather than the waters tag or box collider and lets my character run at full speed because the Submerge.tag != "Water1" any more because it equals the raptor's tag even though I am still underwater.

I would like some help if there is a way to make the water code ignore the raptor but I still have the effect of the raptor sprinting at me when in range.

Comment
Add comment · Show 1
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 MikeNewall · Apr 29, 2014 at 10:40 PM 0
Share

You could set up a collision layer for your water and have it ignore everything but the player.

https://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

3 Replies

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

Answer by Invertex · Apr 29, 2014 at 10:36 PM

Instead of doing an "else if" for being out of water, try using:

 void OnTriggerExit (Collider surfaced)
 {
 if (surfaced.tag == "Water1") 
 {
               _animation ["RunForward"].speed = 1;
               _animation ["RunBackward"].speed = 1;
               _animation ["RunRight"].speed = 1;
               _animation ["RunLeft"].speed = 1;
  
               PlayerMovement1._gravity = 2;
               PlayerMovement1._moveSpeed = 25;
 }
 }

And in your OnTriggerStay, simply make the "else if" an "if", and remove the previous "if".

Full: .

 Animation _animation;
 private PlayerMovement PlayerMovement1;
 void Start ()
 {
     _animation = GetComponentInChildren<Animation> ();
     PlayerMovement1 = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>(); //Just store this once here, instead of calling it each time you enter the water.
 }
 
 void OnTriggerStay (Collider Submerge)
 {
     if (Submerge.tag == "Water1") 
     {
         //Debug.Log ("we Are underwater!");
         _animation ["RunForward"].speed = 0.5f;
         _animation ["RunBackward"].speed = 0.5f;
         _animation ["RunRight"].speed = 0.5f;
         _animation ["RunLeft"].speed = 0.5f;
         
         PlayerMovement1._gravity = 1.0f;
         PlayerMovement1._moveSpeed = 5.0f;
     }
 }
 void OnTriggerExit(Collider surface)
 {
     if (surface.tag == "Water1") 
     {
         _animation ["RunForward"].speed = 1;
         _animation ["RunBackward"].speed = 1;
         _animation ["RunRight"].speed = 1;
         _animation ["RunLeft"].speed = 1;
         
         PlayerMovement1._gravity = 2;
         PlayerMovement1._moveSpeed = 25;        
     }
 }
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 Saravia95 · Apr 29, 2014 at 11:16 PM 0
Share

Thanks this worked perfectly! :D

avatar image
0

Answer by getyour411 · Apr 29, 2014 at 10:35 PM

If I follow, expand the if condition to

|| Raptor.tag)

or something like that

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 LMan · Apr 29, 2014 at 10:41 PM

Well, if you want to know if there's a tag mixup in the code, you can simply throw in Debug.Log(Submerge.tag); Anywhere in OnTriggerStay to get back which tag it's looking at. If it's the wrong one you could use collision layers to restrict which tags are detectable.

I would highly recommend using OnTriggerEnter and Exit rather than stay. If I'm not mistaken, all those variables only need to be set once when you enter the water, and reset when you exit the water.

Rather than setting the speed variable to a specific value, you could add say .3 to the speed whenever the raptor sprints. That way it's not resetting to a hardcoded number.

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 LMan · Apr 29, 2014 at 10:42 PM 0
Share

And you would only add to the speed once when the raptor starts sprinting! Not every frame, that wouldn't end well lol.

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Something Wrong With My Script 0 Answers

Ontriggerenter not working 1 Answer

Can't convert int to bool? 2 Answers

How to make a smooth transition between Vector3's 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