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 /
avatar image
1
Question by superluigi · Jun 10, 2013 at 11:52 PM · javascriptcharacter controllernoob

Help with CharacterController rotation

Hi I think this is an easy fix however I'm having trouble with it. Basically I need my CharacterController to face the same direction hes moving.

 var speed : float = 4.0;
 var gravity : float = 20.0;
 private var velocity : Vector3 = Vector3.zero;
 var controller : CharacterController;
 
 function Update () 
 {
     if (controller.isGrounded)
     {
         velocity = GetHorizontalMovement();
         velocity *= speed;
     }
 
 velocity.y -= gravity * Time.deltaTime;
 controller.Move (velocity * Time.deltaTime);
 }
 
 function GetHorizontalMovement()
 {
 var direction : Vector3 = Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0);
 return transform.TransformDirection (direction);
 }
Comment
Add comment · Show 6
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 superluigi · Jun 12, 2013 at 09:52 PM 0
Share

This is for a 2D game

avatar image InfiniBuzz · Jun 12, 2013 at 10:00 PM 0
Share

what kind of charactercontroller are you using?

avatar image InfiniBuzz · Jun 12, 2013 at 10:08 PM 0
Share

other question :) Is that a sidescroller or a top-down game? It looks like a sidescroller since you only checking the Horizontal axis? in this case you could just check if the Horizontal-Axis value is >0 or <0 and then rotate the character

avatar image superluigi · Jun 12, 2013 at 10:18 PM 0
Share

Im not sure what you mean by what kind of character controller. I only know of the one in the physics component. The game is a sidescroller.

avatar image superluigi · Jun 12, 2013 at 10:26 PM 0
Share

The problem with that is if i go left its <0 and itll turn 180 but if I press left again it's still <0 so it will turn again when it shouldn't

Show more comments

1 Reply

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

Answer by InfiniBuzz · Jun 12, 2013 at 10:36 PM

you can use (not tested)

 function Update ()
 {
    if (controller.isGrounded)
    {
       velocity = GetHorizontalMovement();
       velocity *= speed;
    }
  
    velocity.y -= gravity * Time.deltaTime;
    controller.Move (velocity * Time.deltaTime);
    if(Input.GetAxis("Horizontal") > 0)
       transform.rotation = Quaternion.Euler(0, 0, 0);
    else if(Input.GetAxis("Horizontal") < 0)
       transform.rotation = Quaternion.Euler(0, 180, 0);
 }

You will need to adjust the rotating values sice I don't know how your scene is set up.

E: I see you are a quiet new user, make sure to tick and vote answers that helped you on unity answers (also on your other questions) this helps others to see if an answer is sucessfully answered and the person you tick gets more karma ;)

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 InfiniBuzz · Jun 12, 2013 at 10:40 PM 0
Share

Edited the answer ;)

avatar image superluigi · Jun 12, 2013 at 10:51 PM 0
Share

It worked!!! When I go left he faces left but walks backwards but you fixed the rotation which has been killing me for 3 days thanks a lot brother.

avatar image InfiniBuzz · Jun 12, 2013 at 10:53 PM 0
Share

Glad it helped :) if its facing wrong you need to adjust the angles or the axis input (0)

make sure to tick and vote answers if they helped :)

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

15 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

Related Questions

Trouble converting player controller from JS to C# 1 Answer

Collider help (read description) 2 Answers

Javascript for Character Controller 2 Answers

Make a global variable that can be accessed in different scenes 2 Answers

Game character wont stop jumping 1 Answer


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