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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by MuseumTourGames · Aug 17, 2013 at 05:16 PM · 2drotationcollisioncharactercontroller

CharacterController "breaks" collision, goes haywire

Setup: I have a 2D XZ planed world that my character controller moves around in (top-down view). I have a tilemap with many mesh colliders (rectangular).

Question: Whenever I place polygonal colliders in the world (of any type) my character can seemingly "break" through them. When I push up against them for long enough, my character just "snaps" and starts rotating rapidly around the Y axis (3D space), or goes invisible. How do I prevent this?

What I've already tried: I've tried increasing the collider depth of all objects involved in these scenarios, I have attached a rigidbody and locked rotation on the X and Z axes, and locked position on the Y axis, and I have manually tried updating the EulerAngles every frame by setting the Y rotation to 0 every frame. This appears to help somewhat, yet still the character "breaks".

I find this sort of humorous, if the character tries hard enough, he can somehow break the rules of programming, and enter into the 3D world...VERY frustrating.

Here's the character script, if it helps:

 using UnityEngine;
 using System.Collections;
 
 public class DungeonPlayerBehaviorScript : MonoBehaviour {
     private const float VELOCITY = 40;
     private const float ROTATION_SPEED = 1024.0f;  // Degrees per second
     
     private CharacterController controller;
     private Vector3 direction;
     private tk2dSpriteAnimator animator;
     
     private Quaternion qTo;
     
     private bool joystickConnected;
     
     void Start () {
         //ONLY FOR TESTING PURPOSES
         //TODO: Make multiplayer-compatible joystick and input system
         if (Input.GetJoystickNames ().Length > 0) {
             joystickConnected = true;
         } else {
             joystickConnected = false;
         }
         
         animator = GetComponent<tk2dSpriteAnimator>();
         
         Light2D.RegisterEventListener (LightEventListenerType.OnEnter, onBeamEnter);
     }
     
     void Update () {
         updateFrameVariables();
         
         if (joystickConnected) {
             joystickMovementUpdate();
         } else {
             keyboardMovementUpdate();
         }
         
         if (controller.velocity.sqrMagnitude > 0) {
             animator.Play ("forward");
         } else {
             animator.Play ("idle");
         }
     }
     
     private void joystickMovementUpdate() {
         if (direction.sqrMagnitude > 0.1f) {
             qTo = Quaternion.LookRotation(direction);
             qTo.eulerAngles = new Vector3(90, qTo.eulerAngles.y + 90, 0);
         }
         transform.eulerAngles = new Vector3(90, transform.eulerAngles.y,0);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, ROTATION_SPEED * Time.smoothDeltaTime);
         controller.Move (new Vector3(Input.GetAxis ("Left Joystick Horizontal") * VELOCITY, 0, -Input.GetAxis ("Left Joystick Vertical") * VELOCITY) * Time.smoothDeltaTime);
     }
     
     private void keyboardMovementUpdate()  {
         transform.LookAt (Camera.main.ScreenToWorldPoint (Input.mousePosition));
         controller.Move (new Vector3(Input.GetAxis ("Keyboard Horizontal") * VELOCITY, 0, Input.GetAxis ("Keyboard Vertical") * VELOCITY) * Time.smoothDeltaTime);
     }
     
     private void updateFrameVariables() {
         controller = GetComponent<CharacterController>();
         direction = new Vector3(Input.GetAxis ("Right Joystick Horizontal"), 0, Input.GetAxis ("Right Joystick Vertical"));
     }
     
     void onBeamEnter(Light2D light, GameObject go) {
         Debug.Log (go.tag);
         if (go.CompareTag ("Mob One")) {
             go.GetComponent<MobOneBehaviorScript> ().State = 1;
         }
     }
     
     void LateUpdate()
     {
         Camera.main.transform.position = new Vector3(transform.position.x, 2, transform.position.z);
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by MuseumTourGames · Aug 18, 2013 at 12:15 AM

The problem was that my CharacterController height was not high enough, and my player was "stepping" over the other object :)

Comment
Add comment · Show 1 · 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 Owen-Reynolds · Aug 18, 2013 at 08:57 PM 0
Share

This can also happen when it is too high. The bottom really counts as being curved, and charCont.$$anonymous$$ove runs physics-like code to decide whether to stepOver/around/getStuck.

It's a little bit trial&error whether a cube is heigh enough to be a wall or is merely a step.

avatar image
0

Answer by Owen-Reynolds · Aug 17, 2013 at 05:40 PM

Sounds like an extra rigidbody on the player is causing the problem. Generally RBs and CC's on the same object fight each other.

Unlike a rigidbody "physics object", a characterController will never decide to just spin (your "rapidly spin around y" issue.) Rotation is completely controlled by the particular script, and most of them are pretty boring -- they rotate a fixed rate L/R based on arrow keys.

Comment
Add comment · Show 1 · 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 MuseumTourGames · Aug 17, 2013 at 06:17 PM 0
Share

Sadly the problem still arises when I remove the rigidbody and only use the character controller...

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

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

top down 3d camera rotation question 2 Answers

How to do a raycast to make my player unable to move through certain objects? 2 Answers

How can I rotate an object towards the mouse in only one axis? 1 Answer

problems with OnControllerColliderHit 1 Answer

[2D] CharacterController rotation 0 Answers


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