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.

If I follow, expand the if condition to

|| Raptor.tag)

or something like that

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;		
	}
}

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.