Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gringo · Jul 13, 2011 at 11:19 PM · 2dcontroller

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);

}

Comment
Add comment · Show 1
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 SilverTabby · Jul 14, 2011 at 12:13 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · 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 Gringo · Jul 14, 2011 at 08:47 AM 0
Share

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!

avatar image aldonaletto · Jul 14, 2011 at 12:06 PM 0
Share

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.

avatar image Gringo · Jul 14, 2011 at 07:46 PM 0
Share

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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