collider that OnControllerColliderHit detects but doesnt effect the characters position?
hey there,
i am currently trying to let my character swim if it is under water. To detect that i thought about theOnControllerColliderHit() function since im using a gamecontroller instead of a rigidbody.
my code looks like this:
void OnControllerColliderHit(ControllerColliderHit hit) {
if (hit.gameObject.tag == "Water") {
isSwimming = true;
}
}
when i apply a collider to the water surface it will detect the collision with the water but the character will of course walk on the water instead of going under it since it is a collider. So what do i have to do?
Thank you!
Answer by gameplay4all · Apr 01, 2017 at 03:05 PM
Use a trigger for the water and use code like this on the water:
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player") {
other.GetComponent<playerScript>().isSwimming = true;
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player") {
other.GetComponent<playerScript>().isSwimming = false;
}
}
You only need to fill in the name of the actual script that has the isSwimming field.
Good luck!
-Gameplay4all
Your answer
Follow this Question
Related Questions
[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6) 1 Answer
create object on raycast collision that follows raycast. 0 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer
How to Make A Character Stop At Wall? 0 Answers
problem with tilemap collider and composite collider 1 Answer