I can not use Collision Detection

Hi, I am trying to make a humanoid swim, my first attempt to get out of water was to compare the borders of the swimming pool, with the character position, but i thought it isn’t the corect way to do it, so I tried to use collision detection, I am breaking my head since then. Could somebody gime a hand with that Please, Thank You.

This is my script:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 9.81F;
    private float swimm = 0.0F;
    public Vector3 moveDirection = Vector3.zero;
    public CharacterController controller;
    public Animator anim;
    public bool water = false;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (controller.isGrounded)
        {
            if (!water)
            {
                swimm = Input.GetAxis("Horizontal");
                if (swimm < 0)
                {
                    anim.SetFloat("Speed", swimm * -1);
                }
                else
                {
                    anim.SetFloat("Speed", swimm);
                }
                swimm = Input.GetAxis("Vertical");
                if (swimm < 0)
                {
                    anim.SetFloat("Speed", swimm * -1);
                }
                else
                {
                    anim.SetFloat("Speed", swimm);
                }

                moveDirection = new Vector3(0, 0, swimm);
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;

                if (Input.GetKey(KeyCode.UpArrow))
                {
                    anim.ResetTrigger("Idle");
                    anim.SetTrigger("Walk");
                }

                if (Input.GetKeyUp(KeyCode.UpArrow))
                {
                    anim.ResetTrigger("Walk");
                    anim.SetTrigger("Idle");
                }

                if (Input.GetKey(KeyCode.DownArrow))
                {
                    anim.ResetTrigger("Idle");
                    anim.SetTrigger("Walk");
                }

                if (Input.GetKeyUp(KeyCode.DownArrow))
                {
                    anim.ResetTrigger("Walk");
                    anim.SetTrigger("Idle");
                }

                if (Input.GetKeyDown(KeyCode.LeftControl))
                    moveDirection.y = jumpSpeed;

                if (transform.position.y < 5.0F)
                {
                    water = true;
                    anim.ResetTrigger("Walk");
                    anim.ResetTrigger("Idle");
                    anim.SetTrigger("Float");
                }
            }
        }

        transform.Rotate(0, Input.GetAxis("Horizontal"), 0);

        if (!water)
        {
            moveDirection.y -= gravity * Time.deltaTime;
            controller.Move(moveDirection * Time.deltaTime);
        }
        else
        {
            swimm = Input.GetAxis("Horizontal");
            if (swimm < 0)
            {
                anim.SetFloat("Speed", swimm * -1);
            }
            else
            {
                anim.SetFloat("Speed", swimm);
            }
            swimm = Input.GetAxis("Vertical");
            if (swimm < 0)
            {
                anim.SetFloat("Speed", swimm * -1);
            }
            else
            {
                anim.SetFloat("Speed", swimm);
            }
            moveDirection = new Vector3(0, 0, swimm);
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            controller.Move(moveDirection * Time.deltaTime);

            transform.position = new Vector3(transform.position.x, 3.6F, transform.position.z);
            if (transform.position.x > 11.9F || transform.position.x < -11.9F || transform.position.z > 6.9F || transform.position.z < -6.9F)
            {
                transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
                water = false;
                anim.ResetTrigger("Float");
                anim.SetTrigger("Idle");
            }
        }
    }

    /*void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.gameObject.tag.Equals("Border"))
        {
            transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
            water = false;
            anim.ResetTrigger("Float");
            anim.SetTrigger("Idle");
        }
    }*/

    /*void OnTriggerEnter(Collider col)
    {
        if (col.tag.Equals("Border"))
        {
            transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
            water = false;
            anim.ResetTrigger("Float");
            anim.SetTrigger("Idle");
        }
    }*/
}

You should definitely stop doing this. It’s bad for your head.

First of all, please use code tags: https://discussions.unity.com/t/481379

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

You need to start a fresh scene, work through a collision example and get the simplest collisions working, a sphere hitting a box for instance, and once you understand all that is required to do collision (and this is all in the Unity docs for the collision functions), then you can go back to your complex swimming example.

1 Like

Thanks Kurt, I sure post right next time, and I will follow your advice of starting from scratch. Thanks again for taking the time to respond.

Finally the method OnControllerColliderHit(ControllerColliderHit hit) worked the way I wanted, with the transform.position.y with a value of 6.1F.

1 Like