Search Unity

Need help detecting collisions.

Discussion in 'Scripting' started by Brandon-Keeler, Nov 21, 2014.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    So I have this compound PlayerController script that does one thing when the character is Kinematic, and another thing when it isn't Kinematic. When my character isn't Kinematic, my character collides perfectly with the floor and stops moving, but when it is Kinematic, it passes right through the floor (which I don't want it to do). Do you think you can help me with that? This is my code...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public GameObject player;
    6.     public float KineticMoveSpeed;
    7.     public float NormalMoveSpeed;
    8.     public float turnSpeed;
    9.     float moveHorizontal;
    10.     float moveHeight;
    11.     float moveVertical;
    12.     float KinematicTimeInAir;
    13.     float Acceleration;
    14.     bool spaceKeyDown = false;
    15.     bool isGrounded = true;
    16.     void Start()
    17.     {
    18.         KinematicTimeInAir = 0.0f;
    19.         Acceleration = -9.81f;
    20.     }
    21.     void Update ()
    22.     {
    23.         moveHorizontal = Input.GetAxis ("Horizontal");
    24.         moveVertical = Input.GetAxis ("Vertical");
    25.         if(Input.GetButtonDown("Jump"))
    26.         {
    27.             spaceKeyDown = true;
    28.             //Debug.Log("Space Key Pressed. Frame: " + Time.frameCount);
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate ()
    33.     {
    34.                 if (player.rigidbody.isKinematic == true) {
    35.                 KinematicPlayerJump (KinematicTimeInAir);
    36.                         transform.Rotate (Vector3.up * moveHorizontal * turnSpeed * Time.deltaTime);
    37.                         transform.Translate (Vector3.forward * moveVertical * KineticMoveSpeed * Time.deltaTime);
    38.                 }
    39.                 else
    40.                 {
    41.                         NormalPlayerJump ();
    42.                         Vector3 movement = new Vector3 (moveHorizontal, moveHeight / NormalMoveSpeed, moveVertical);
    43.                         rigidbody.AddForce (movement * NormalMoveSpeed * Time.deltaTime);
    44.                 }
    45.         moveHeight = 0;//Make sure vertical movement is zeroed after normal jump sequence.
    46.         //Need to set KinematicTimeInAir = 0.0f, if the player collides with the ground
    47.     }
    48.  
    49.     void NormalPlayerJump()
    50.             {
    51.         if (isGrounded == true)
    52.         {
    53.             if (spaceKeyDown == true)
    54.             {
    55.                 spaceKeyDown = false;
    56.                 moveHeight = 9.8f * 1500f;
    57.             }
    58.             else {moveHeight = 0;}
    59.         }
    60.         else {moveHeight = 0;}
    61.         spaceKeyDown = false;
    62.     }
    63.     void KinematicPlayerJump(float time)
    64.     {
    65.         if (spaceKeyDown == true) {
    66.             float VelocityInitial = 9.8f;
    67.             float CurrentVelocity;
    68.             CurrentVelocity = VelocityInitial + Acceleration * time;
    69.             transform.Translate (Vector3.up * (CurrentVelocity) * Time.deltaTime);
    70.             KinematicTimeInAir += (float)(Time.deltaTime);
    71.         }
    72.     }
    73.     void OnCollisionEnter (Collision col)
    74.     {
    75.         if(col.gameObject.tag == "Floor")
    76.         {
    77.             isGrounded = true;
    78.         }
    79.     }
    80.  
    81.     void OnCollisionExit (Collision col)
    82.     {
    83.         if(col.gameObject.tag == "Floor")
    84.         {
    85.             isGrounded = false;
    86.         }
    87.     }
    88. }
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
  3. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I want my character to collide with the stuff around it, can you show me how I can implement that "raycasting" technique into my code?
     
  4. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Actually, have you tried using OnCollisionEnter, OnCollisionExit and OnCollisionStay Methods?
    http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

    I think that might be an easier solution.
    Use OnCollisionEnter method to detect when the character collides with something, and then do whatever you want to do about it.
    Notice that you can also check what your character is colliding with using the collision information this method gives you.

    Make sure you add Collider components to the character and to the objects you want to collide with.
     
  5. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    look at the bottom of my script, they are there, the problem is that I cant detect the collisions while my player isKinematic
     
  6. IronDuke

    IronDuke

    Joined:
    May 13, 2014
    Posts:
    132
    When your rigidbody is Kinematic, collisions simply will not be detected. This is by design, so if you want collisions you have to leave isKinematic off.
    The reason they did that is if you need a rigidbody for stuff like Joints, but don't want it reacting to physics.
    By the way, there is a Character Controller built into Unity already if you want to take a look at that.
     
  7. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    Even if it doesn't detect collisions, is there a way to get my character to stop moving in a direction, once the edge of my player touches an object.