2D Toolkit Forum
2D Toolkit => Support => Topic started by: JBabz on August 17, 2013, 06:12:56 pm
-
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.
-
What did you add a rigidbody to? What do you have on your character controller?
-
This is the character's components.
-
Why do you have a rigidbody on it?
-
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);
}
}
-
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.
-
Here, this is the project, the scene is Assets/Game/scn/Dungeon.unity
Haha, I'm soooo frustrated..
Edit: Removed URL.
-
How do I reproduce this in your sample?
-
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).
-
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.
-
That worked! Thank you so much :)