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 dsgsgsg · Oct 11, 2013 at 02:34 PM · rotationplayercharacter

Character Controller rotate unity

I am using a joystick to make the character move using the CHaracter Controller script like this:

 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var moveJoystick : Joystick;
 var speed: float = 1.0;
 var turnSpeed:float = 1.0;
 var angle: float = 1.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
         var controller : CharacterController = GetComponent(CharacterController);
         moveDirection.y -= gravity * Time.deltaTime;
 
         if (moveJoystick.position.y>0) {
                 controller.Move(Vector3.forward * moveJoystick.position.y * speed);
         }
         else { 
                 controller.Move(-Vector3.forward * moveJoystick.position.y * speed); 
         }
         controller.Move(moveDirection * Time.deltaTime);
         angle = Mathf.Atan2(moveJoystick.position.x, transform.rotation = Quaternion.Euler(new Vector3(0, angle,0));
 }

The only problem is that the player does not move towards the direction it is facing. It just keeps going forward all the time.

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 mattssonon · Oct 11, 2013 at 02:35 PM 0
Share

Please format your code so it is readable.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Oct 11, 2013 at 02:53 PM

On line 15 and line 18 replace 'Vector3.forward' with 'transform.forward'. Vector3.forward is shorthand for Vector(0,0,1) and represents the forward direction in the local coordinates of the object. 'transform.forward' is forward in world coordinates. It is the same as:

 transform.TransformDirection(Vector3.forward);
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 dsgsgsg · Oct 11, 2013 at 03:00 PM 0
Share

thank you that works. Although just one thing, that the rotation snaps when joystick is released to angles like 90, 180, 270 ,360

avatar image robertbu · Oct 11, 2013 at 04:09 PM 1
Share

I'm guessing about your joystick code. First thought is to create a threshold for the joystick. Your code sets the angle even when the joystick is likely reporting Vector3.zero.

 if (moveJoystick.position.magnitude > someSmallValue)
     angle = $$anonymous$$athf.Atan2(moveJoystick.position.x, moveJoystick.position.y)
     transform.rotation = Quaternion.Euler(new Vector3(0, angle,0));
 }

Another strangeness here. Atan2() takes Y,X. That is it is ually called with something like:

 angle = $$anonymous$$athf.Atan2(v3.y, v3.x);

  
avatar image dsgsgsg · Oct 11, 2013 at 04:14 PM 0
Share

thanks man although using: $$anonymous$$athf.Atan2(v3.y, v3.x); got me a weird movement problem. It gets messed up. Also the angle issue got fixed like: if (moveJoystick.position.x!=0){getAngle......}

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

16 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

Related Questions

Character Rotation 2 Answers

A node in a childnode? 1 Answer

Unity List? 1 Answer

rotating at mouse 0 Answers

Character rotation on foot? 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