Character not colliding with other objects

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) :

public class Bounce : MonoBehaviour {

    float lerpTime;
    float currentLerpTime;
    float perc = 1;

    Vector3 startPos;
    Vector3 endPos;

    bool firstInput;
    public bool justJump;

    public GameObject player;


    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
            if (perc == 1) {
                lerpTime = 1;
                currentLerpTime = 0;
                firstInput = true;
                justJump = true;
            }
        }
        startPos = gameObject.transform.position;

 
        if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
            endPos = transform.position + player.transform.rotation * (new Vector3(0, 0, 1f));

        }
        if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
            endPos = transform.position + player.transform.rotation * (new Vector3(0, 0, -1f)); 
        }

        if (firstInput == true) {
            currentLerpTime += Time.deltaTime * 5;
            perc = currentLerpTime / lerpTime;
            gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
            if (perc > 0.8f)  {
                perc = 1;
            }

            if (Mathf.Round(perc) == 1) {
                justJump = false;
            }
        }


    }

    void OnCollisionEnter(Collision collision) {
        Debug.Log("!!!!!!!");
    }
}

And I use this script on the character itself: (to rotate and animate it)

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        Bounce bounceScript = thePlayer.GetComponent<Bounce>();
        if (bounceScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            //transform.rotation *= Quaternion.Euler(0,30,0);
            transform.RotateAround(transform.position, Vector3.up, 90);
        }
        if (Input.GetButtonDown("left")) {
            transform.Rotate (0, -90, 0, 0);
        }
    }
}

Any help would be appreciated!
Thanks!

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.

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?

I’ll see what I can do for you

Thanks in advance!

Have you solved it yet Zwiebel we watched the same videos based on Crossy Road in unity i have the same problem as you

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.