Search Unity

Character not colliding with other objects

Discussion in 'Physics' started by Zwiebel, May 9, 2015.

  1. Zwiebel

    Zwiebel

    Joined:
    Jul 23, 2013
    Posts:
    56
    Hi guys!

    I'm working on a game right now (first 3D styled game), and I have a problem with my character colliding. I have a Player object, which has another object(s) as the real moveable characters (now I have only one). I have rigidbody and box collider too attached to my character. I have made a level (with hand-placed platforms), and I would like avoid my character of falling of the platforms, while the user control it. I have tried to place a cube on the side of the platform(s), with box collider attached to it, but the character not detect the colliding for some reasons. I would like my character to be stopped by the collider "cube", but it doesn't happen.

    I use this script to move my object (attached to my character Player object) :

    Code (csharp):
    1.  
    2. public class Bounce : MonoBehaviour {
    3.  
    4.     float lerpTime;
    5.     float currentLerpTime;
    6.     float perc = 1;
    7.  
    8.     Vector3 startPos;
    9.     Vector3 endPos;
    10.  
    11.     bool firstInput;
    12.     public bool justJump;
    13.  
    14.     public GameObject player;
    15.  
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
    20.             if (perc == 1) {
    21.                 lerpTime = 1;
    22.                 currentLerpTime = 0;
    23.                 firstInput = true;
    24.                 justJump = true;
    25.             }
    26.         }
    27.         startPos = gameObject.transform.position;
    28.  
    29.  
    30.         if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
    31.             endPos = transform.position + player.transform.rotation * (new Vector3(0, 0, 1f));
    32.  
    33.         }
    34.         if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
    35.             endPos = transform.position + player.transform.rotation * (new Vector3(0, 0, -1f));
    36.         }
    37.  
    38.         if (firstInput == true) {
    39.             currentLerpTime += Time.deltaTime * 5;
    40.             perc = currentLerpTime / lerpTime;
    41.             gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
    42.             if (perc > 0.8f)  {
    43.                 perc = 1;
    44.             }
    45.  
    46.             if (Mathf.Round(perc) == 1) {
    47.                 justJump = false;
    48.             }
    49.         }
    50.  
    51.  
    52.     }
    53.  
    54.     void OnCollisionEnter(Collision collision) {
    55.         Debug.Log("!!!!!!!");
    56.     }
    57. }
    58.  
    And I use this script on the character itself: (to rotate and animate it)

    Code (csharp):
    1.  
    2. public class AnimationController : MonoBehaviour {
    3.  
    4.     Animator anim;
    5.     public GameObject thePlayer;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         anim = gameObject.GetComponent<Animator>();
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         Bounce bounceScript = thePlayer.GetComponent<Bounce>();
    15.         if (bounceScript.justJump == true) {
    16.             anim.SetBool("Jump", true);
    17.         }
    18.         else {
    19.             anim.SetBool("Jump", false);
    20.         }
    21.  
    22.         if (Input.GetButtonDown("right")) {
    23.             //transform.rotation *= Quaternion.Euler(0,30,0);
    24.             transform.RotateAround(transform.position, Vector3.up, 90);
    25.         }
    26.         if (Input.GetButtonDown("left")) {
    27.             transform.Rotate (0, -90, 0, 0);
    28.         }
    29.     }
    30. }
    31.  
    Any help would be appreciated!
    Thanks!
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    If you want collision you have two options;

    1. Keep your current movement style and use raycasting for collision detection
    2. Abandon your current movement style and adopt one that uses rigidbody.AddForce() or rigidbody.velocity

    When you manually assign the position of a GameObject, rigidbody or not, it will pass through objects. Simply put, your movement code is forcing the player through the cube.
     
  3. Zwiebel

    Zwiebel

    Joined:
    Jul 23, 2013
    Posts:
    56
    Thanks the answer!
    I have tried to add rigidbod.AddForce to it, but I couldn't make it move. Could you maybe improve my code with it, on the appropriate places, please?
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I'll see what I can do for you
     
  5. Zwiebel

    Zwiebel

    Joined:
    Jul 23, 2013
    Posts:
    56
    Thanks in advance!
     
  6. HypeerX

    HypeerX

    Joined:
    Mar 28, 2017
    Posts:
    1
    Have you solved it yet Zwiebel we watched the same videos based on Crossy Road in unity i have the same problem as you
     
  7. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    Do you have Colliders attached to your objects? Further, do you have Rigidbody attached? Those are both needed in order to use Velocity.

    Since you're using a grid-based movement system, you simply want to check the cell you're about to move to and if there's something already there then don't allow your character to move. Forgot using the physics engine.