- Home /
Character Controller, wrong direction
Ive made a simple 2d character controller script as shown below, moving on the y axis works fine but when moving horizontally the character always shoots up in the air, and very very slowly moves to the left and right. Any ideas whats wrong with it? thanks!
script:
// Require a character controller to be attached to the same game object @script RequireComponent(CharacterController)
// The last collision flags returned from controller.Move private var collisionFlags : CollisionFlags;
function Update () { var movement:Vector3; movement = Vector3.zero ;
// Pick speed modifier
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
movement = Vector3 (h , v, 0 ) ;
movement = transform.TransformDirection(movement);
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
collisionFlags = controller.Move(movement * Time.deltaTime);
}
Just off the top of my head, it seems like this is your problem:
movement = transform.TransformDirection(movement);
If it's a 2d game, and up is always up for all objects, why are you changing which way up points?
On an unrelated note, this line of code
var controller : CharacterController = GetComponent(CharacterController);
Should NOT be called every frame like you have here. GetComponent is a slow function, and the characterController object is not being dynamically destroyed and rebuilt, so there is no need to call it more than once. $$anonymous$$ake a private/protected variable for the script to hold it, and find it in the Start or Awake functions.
Answer by aldonaletto · Jul 14, 2011 at 12:55 AM
I tried your script, and it's ok: the character moved up, down, right and left as expected. Maybe the problem is in your character: I once childed a cube to the character, and its collider apparently confused the character controller, making the movement very weird - it got blocked to some direction, or moved diagonally, or moved too fast. The problem only disappeared when I removed the collider.
Could it be your case? Have you childed some object to the character? If such object had a collider intersecting the character, strange things may happen.
Thanks aldonaletto, you were right it was my collider that was causing the issue!,Thanks it was my collider, i didnt realise that would cause issues!
I suspect this is a Character Controller bug. A collider added to the Character Controller should become part of it, not a problem! I'll report it as a bug. By the way, if this answer solved your problem, click the "check" button under the answer voting buttons to signal the question was solved - it will help the others to solve similar problems.
Ive added another mesh collider now and it seems to work fine, weird though, as i reset the previous collider that was causing the issues. :S
Your answer
![](https://koobas.hobune.stream/wayback/20220612142926im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2d Platform Controller Face Help 1 Answer
2D game movement/jump issue 2 Answers
Problem creating a 2D scroller shooting game 2 Answers
How to use multiple bone rigged sprites for 2d character animations 0 Answers
Character Controller Jittering Problem 2 Answers