CharacterController Component Collision Problem

Ok, so… I’m trying to learn about the character controller component. I have a player game object with a Rect Transform, SpriteRenderer, CharacterController, and a C# Script. So far, the only thing the script does is simulate gravity, so when the game is played the player game object falls down. This works fine. However, when I add another game object to the scene called ground with only a Transform and an EdgeCollider2D, and I position the player above it, the player falls right through it. It is as if the character controller does not detect the collision at all. What am I misunderstanding?

Here’s the player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private float gravity = 9.81f;
    private float verticalSpeed = 0;

    private CharacterController characterController;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    private void Update()
    {
        verticalSpeed -= gravity * Time.deltaTime;
        Vector2 velocity = new Vector2(0, verticalSpeed);
        characterController.Move(velocity * Time.deltaTime);
    }
}

Attempted solutions:

  • Added a Rigidbody2D component to the ground object.

PS: The game is in 2D.

Bump

The CharacterController is for 3d only, and to my knowledge there is no built in 2d character controller. The unity standard assets might have a demo of a 2d character controller, but if not then youll just have to find one somewhere, make your own, or maybe try and use the 2d rigidbody for your player.
I dont know much about 2d so thats about all I can help =/

Ah, that makes a lot of sense. Thanks :slight_smile:

Yep, I believe there is a 2d character in the standard assets.
One other note, though, I don’t think you should have a rect transform on your 2d character. Rect transforms are for canvas objects, but I would imagine that you want your character to be a sprite (on also not in the canvas) :slight_smile:

You’re right. I’m not sure why I was using a rect transform, but now I’ve changed it to a regular transform. Thanks :slight_smile:

Cool, no prob :slight_smile: