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
0
Question by Bincredible · Sep 28, 2013 at 05:46 PM · rotationmovementcharactercontrollerdirection

Help with my Character Controller

Hey, I am struggling to find a way to make the player rotate to its current direction. For example if I press the down arrow, the player would face the camera and walk towards it. Or if I pressed right the player would face and walk right. Here is my current script :

     var speed : float = 6.0;
     var jumpSpeed : float = 8.0;
     var gravity : float = 20.0;
 
     private var moveDirection : Vector3 = Vector3.zero;
 
     function Update() {
         var controller : CharacterController = GetComponent(CharacterController);
         if (controller.isGrounded) {
             moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                     Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }

I have tried many methods but simply can't find a way to do it, any help would be appreciated.

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 ShadoX · Sep 28, 2013 at 06:31 PM 0
Share

can't test the code, but I'd suggest to get the camera objects forward vector (transform.forward) and use that to figure out which way the character should face

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gardian06 · Sep 28, 2013 at 06:31 PM

So if I am understanding your context correctly then you are simply wanting the transform.forward to become equal to either Vector3.forward, Vector3.back, Vector3.left, or Vector3.right depending on if the player has pressed 1, or 4 buttons, and probably not blending these together (if you do want to blend then then things get a little harder), and then move the character in that direction.

essentially I am seeing:

 if(Input.GetAxis("Vertical") < 0){
     transform.forward = Vector3.back;
 }else if(Input.GetAxis("Vertical") > 0){
     transform.forward = Vector3.forward;
 }else if(Input.GetAxis("Horizontal") < 0){
     transform.forward = Vector3.left;
 }else if(Input.GetAxis("Horizontal") > 0){
     transform.forward = Vector3.right;
 }
 moveDirection = transform.forward;
 // do gravity work here
 controller.Move(moveDirection * Time.deltaTime); // I would suggest adding in some multiplication factor because Time.deltaTime is usually rather small (somewhere between .01, and maxTimestep)

the next suggestion would be to store the transform so that you don't have to keep looking it up for performance

Comment
Add comment · Show 4 · 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 Bincredible · Sep 28, 2013 at 07:16 PM 0
Share

Ok, this works, just you can go at 45 degree angles.

avatar image gardian06 · Sep 28, 2013 at 07:47 PM 0
Share

do you mean that you want them all to be 45 degrees from what they are, or you also want the ability to go at 45 degree angles as well?

avatar image Bincredible · Sep 28, 2013 at 11:42 PM 0
Share

I mean if you press up then down, the player would smoothly go from forward to backward, and if you press right and up the player would turn to a 45 degree angle smoothly from the last position. Basically like a LEGO Game's controller. Which I imagine is very complicated.

avatar image gardian06 · Sep 30, 2013 at 06:38 PM 0
Share

I am still not clear on if you only want to limit this to 90 degree rotations, or you now want 45 degree rotations, but if you want smooth rotations then you will probably want to store a Vector3 (targetForward) set that as the desired forward direction, then either Vector3.Lerp, or Vector3.RotateToward the forward vector to the target vector.

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

17 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 avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Jaggie, uneven speed with basic movement script. 0 Answers

Look rotation viewing is zero warning 0 Answers

Camera relative movement 0 Answers

How do I keep my character facing the direction of travel after movement stops? 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