- Home /
Collision between Character Controller and Box Collider
Hello, guys. I'm developing a 2D platform game and I have already coded a Character Controller script (just Box Collider + Character Controller). Now I want to make a floating platform that moves from horizontally. The problem is that I can't detect the collision between the player and the platform. I know that this kind of question is in the forum but any post could help me until now.
You should further explain your situation. I'm assu$$anonymous$$g you know about OnCollisionEnter() etc. perhaps post some example code RELATED to what you're attempting?
It falls through the platform? Or just wont follow it when it moves?
Answer by sombra · Jan 11, 2013 at 12:12 AM
Actually, the Platform uses just a Box Collider but I can't detect the collision neither by OnCollisionEnter nor by OnTriggerEnter(). The player stays over the platform and the platform moves but the player doesn't (because of this I want to test the collision).
The player Character Controller code is:
using UnityEngine;
using System.Collections;
public class PlayerBehavior : MonoBehaviour {
public float speed = 5f;
public float jumpSpeed = 6f;
private float gravity = -5f;
public Vector3 movement;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
//le a direcao de movimento
if (Input.GetKey(KeyCode.LeftArrow))
movement.x = -speed;
if (Input.GetKey(KeyCode.RightArrow))
movement.x = speed;
if (Input.GetKeyDown(KeyCode.UpArrow) && controller.isGrounded)
movement.y = jumpSpeed;
if (!controller.isGrounded)
{
//s -= gt^2
movement.y += gravity * Time.deltaTime;
if (movement.y < -20f)
movement.y = -20f;
}
controller.Move(movement * Time.deltaTime);
movement.x = 0;
if ((controller.collisionFlags & CollisionFlags.CollidedAbove) != 0)
movement.y = -0.7f;
}
}
and the platform code:
public class FloatingPlatformBehavior : MonoBehaviour
{
public float speed = 1f;
public float origin, destiny;
private float percorrido;
// Use this for initialization
void Start()
{
percorrido = origin;
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
percorrido += speed * Time.deltaTime;
if (percorrido >= destiny || percorrido <= origin)
speed *= -1;
}
void OnCollisionEnter(Collision other)
{
Debug.Log("Colidiu");
}
}
Answer by TomPendergrass · Jan 11, 2013 at 02:57 AM
I do believe that for OnCollisionEnter and OnTriggerEnter to work, at least one of the colliders must be a rigidbody. I would suggest setting your platform to a kinematic rigidbody. Setting it to kinematic will allow you to still use the object as if it weren't susceptible to the physics calculations.
Thank you so much, $$anonymous$$ but let me ask you one last question. Adding a $$anonymous$$inematic Rigidbody to my platform will let me use OnTrigger functions or OnCollision functions? Thanks again.
Actually, I've tried what you said and didn't work. I added a kinematic rigidbody to the platform and added this to it's script:
void OnCollisionEnter(Collision other) { Debug.Log("Col1"); }
void OnTriggerStay(Collider other)
{
Debug.Log("Col2");
}
But none of these logs have appeared in my Console.
OnTrigger would work if you put the trigger on the platform, and the rigidbody on your player (I don't believe the CharacterController actually counts as a physics object) I do believe OnCollisionEnter would work. I really hope the solution to your problem comes to you soon.
You're right. Character Controller doesn't count as a physics object) but if I put the trigger on the platform the player will run trough the object and this not help. Am I right?
Yes I would not suggest using a trigger for a platform lol
Actually, Unit's $$anonymous$$anual says that for the OnCollisionEnter() works one of the bodies must have a NON $$anonymous$$innematic Rigidbody. Sorry for bother you so much but I'm getting really unmotivated with this =(
Your answer
Follow this Question
Related Questions
Pushing against Edge Collider wall stops player from falling 0 Answers
How to prevent Character Controller from falling through moving Platforms? 4 Answers
One-way platform's side causes collision with Platform Effector 1 Answer
2d collision with "unwalkable" objects 2 Answers
Box collider with rigid body yet enemies still pass through each other. 1 Answer