Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I can not use Collision Detection

Discussion in 'Scripting' started by Orions68, Dec 22, 2020.

  1. Orions68

    Orions68

    Joined:
    Nov 16, 2020
    Posts:
    3
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public float speed = 6.0F;
    6.     public float jumpSpeed = 8.0F;
    7.     public float gravity = 9.81F;
    8.     private float swimm = 0.0F;
    9.     public Vector3 moveDirection = Vector3.zero;
    10.     public CharacterController controller;
    11.     public Animator anim;
    12.     public bool water = false;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         controller = GetComponent<CharacterController>();
    18.         anim = GetComponent<Animator>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (controller.isGrounded)
    25.         {
    26.             if (!water)
    27.             {
    28.                 swimm = Input.GetAxis("Horizontal");
    29.                 if (swimm < 0)
    30.                 {
    31.                     anim.SetFloat("Speed", swimm * -1);
    32.                 }
    33.                 else
    34.                 {
    35.                     anim.SetFloat("Speed", swimm);
    36.                 }
    37.                 swimm = Input.GetAxis("Vertical");
    38.                 if (swimm < 0)
    39.                 {
    40.                     anim.SetFloat("Speed", swimm * -1);
    41.                 }
    42.                 else
    43.                 {
    44.                     anim.SetFloat("Speed", swimm);
    45.                 }
    46.  
    47.                 moveDirection = new Vector3(0, 0, swimm);
    48.                 moveDirection = transform.TransformDirection(moveDirection);
    49.                 moveDirection *= speed;
    50.  
    51.                 if (Input.GetKey(KeyCode.UpArrow))
    52.                 {
    53.                     anim.ResetTrigger("Idle");
    54.                     anim.SetTrigger("Walk");
    55.                 }
    56.  
    57.                 if (Input.GetKeyUp(KeyCode.UpArrow))
    58.                 {
    59.                     anim.ResetTrigger("Walk");
    60.                     anim.SetTrigger("Idle");
    61.                 }
    62.  
    63.                 if (Input.GetKey(KeyCode.DownArrow))
    64.                 {
    65.                     anim.ResetTrigger("Idle");
    66.                     anim.SetTrigger("Walk");
    67.                 }
    68.  
    69.                 if (Input.GetKeyUp(KeyCode.DownArrow))
    70.                 {
    71.                     anim.ResetTrigger("Walk");
    72.                     anim.SetTrigger("Idle");
    73.                 }
    74.  
    75.                 if (Input.GetKeyDown(KeyCode.LeftControl))
    76.                     moveDirection.y = jumpSpeed;
    77.  
    78.                 if (transform.position.y < 5.0F)
    79.                 {
    80.                     water = true;
    81.                     anim.ResetTrigger("Walk");
    82.                     anim.ResetTrigger("Idle");
    83.                     anim.SetTrigger("Float");
    84.                 }
    85.             }
    86.         }
    87.  
    88.         transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
    89.  
    90.         if (!water)
    91.         {
    92.             moveDirection.y -= gravity * Time.deltaTime;
    93.             controller.Move(moveDirection * Time.deltaTime);
    94.         }
    95.         else
    96.         {
    97.             swimm = Input.GetAxis("Horizontal");
    98.             if (swimm < 0)
    99.             {
    100.                 anim.SetFloat("Speed", swimm * -1);
    101.             }
    102.             else
    103.             {
    104.                 anim.SetFloat("Speed", swimm);
    105.             }
    106.             swimm = Input.GetAxis("Vertical");
    107.             if (swimm < 0)
    108.             {
    109.                 anim.SetFloat("Speed", swimm * -1);
    110.             }
    111.             else
    112.             {
    113.                 anim.SetFloat("Speed", swimm);
    114.             }
    115.             moveDirection = new Vector3(0, 0, swimm);
    116.             moveDirection = transform.TransformDirection(moveDirection);
    117.             moveDirection *= speed;
    118.             controller.Move(moveDirection * Time.deltaTime);
    119.  
    120.             transform.position = new Vector3(transform.position.x, 3.6F, transform.position.z);
    121.             if (transform.position.x > 11.9F || transform.position.x < -11.9F || transform.position.z > 6.9F || transform.position.z < -6.9F)
    122.             {
    123.                 transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
    124.                 water = false;
    125.                 anim.ResetTrigger("Float");
    126.                 anim.SetTrigger("Idle");
    127.             }
    128.         }
    129.     }
    130.  
    131.     /*void OnControllerColliderHit(ControllerColliderHit hit)
    132.     {
    133.         if (hit.gameObject.tag.Equals("Border"))
    134.         {
    135.             transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
    136.             water = false;
    137.             anim.ResetTrigger("Float");
    138.             anim.SetTrigger("Idle");
    139.         }
    140.     }*/
    141.  
    142.     /*void OnTriggerEnter(Collider col)
    143.     {
    144.         if (col.tag.Equals("Border"))
    145.         {
    146.             transform.position = new Vector3(transform.position.x, 5.0F, transform.position.z);
    147.             water = false;
    148.             anim.ResetTrigger("Float");
    149.             anim.SetTrigger("Idle");
    150.         }
    151.     }*/
    152. }
     
    Last edited: Dec 23, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You should definitely stop doing this. It's bad for your head.

    First of all, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
    Orions68 likes this.
  3. Orions68

    Orions68

    Joined:
    Nov 16, 2020
    Posts:
    3
    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.
     
  4. Orions68

    Orions68

    Joined:
    Nov 16, 2020
    Posts:
    3
    Finally the method OnControllerColliderHit(ControllerColliderHit hit) worked the way I wanted, with the transform.position.y with a value of 6.1F.
     
    Kurt-Dekker likes this.