Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sombra · Jan 10, 2013 at 11:10 PM · 2dcollisioncharacter controllerplatformbox collider

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TomPendergrass · Jan 10, 2013 at 11:18 PM 0
Share

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?

avatar image Vonni · Jan 10, 2013 at 11:18 PM 0
Share

It falls through the platform? Or just wont follow it when it moves?

avatar image sombra · Jan 11, 2013 at 12:19 AM 0
Share

The code is below =)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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");
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sombra · Jan 11, 2013 at 03:17 AM 0
Share

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.

avatar image TomPendergrass · Jan 11, 2013 at 03:55 AM 0
Share

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.

avatar image sombra · Jan 11, 2013 at 03:26 PM 0
Share

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?

avatar image TomPendergrass · Jan 11, 2013 at 04:19 PM 0
Share

Yes I would not suggest using a trigger for a platform lol

avatar image sombra · Jan 11, 2013 at 05:32 PM 0
Share

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 =(

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges