Hello Guest

Author Topic: [FIXED] [2.1] CharacterController "breaks" collision, goes haywire  (Read 7180 times)

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
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.
« Last Edit: August 18, 2013, 01:09:39 am by JBabz »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #1 on: August 17, 2013, 06:21:40 pm »
What did you add a rigidbody to? What do you have on your character controller?

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #2 on: August 17, 2013, 06:25:01 pm »
This is the character's components.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #3 on: August 17, 2013, 06:31:13 pm »
Why do you have a rigidbody on it?

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #4 on: August 17, 2013, 07:27:53 pm »
To see if using the rigidbody's "constrain rotation" feature would work. I removed the rigidbody and tried to see if it'll work again..still doesn't. Ugh.
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);
   }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #5 on: August 17, 2013, 08:12:23 pm »
I'm running out of things to suggest. Send me a repro case if you'd like me to take a look at it? Should make things a whole lot easier.

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #6 on: August 17, 2013, 10:22:35 pm »
Here, this is the project, the scene is Assets/Game/scn/Dungeon.unity

Haha, I'm soooo frustrated..
Edit: Removed URL.
« Last Edit: August 17, 2013, 10:27:06 pm by unikronsoftware »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #7 on: August 17, 2013, 10:42:20 pm »
How do I reproduce this in your sample?

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #8 on: August 17, 2013, 10:50:08 pm »
Go north for a little bit, and you will see a red, elliptical monster (which does nothing).
He has a polygonal collider attached to him, so walk "into" him for awhile, and you will see your character "disappear" (he rotates into 3D space).

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #9 on: August 17, 2013, 11:12:58 pm »
It looks like your character is just walking over the other one? Your character controller is effectively a sphere as the height is really low, so the slightest movement or precision error on the y axis results in it starting to "step over" the other object, which also happens to be a sphere as the height is really low.

Try increasing height on the character controller and see how that works. You can bash some really big number in there, like 50 or 100 or something.

« Last Edit: August 18, 2013, 07:30:14 am by unikronsoftware »

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Re: [2.1] CharacterController "breaks" collision, goes haywire
« Reply #10 on: August 18, 2013, 01:09:24 am »
That worked! Thank you so much :)