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 PrimeSoul · Aug 10, 2019 at 11:27 AM · cameramovementmobilecontroller

Mobile Character controller Moving in world space

So i have been attempting and looking for answers about this for the last week and i finally gave up. My last resort is this place. Im creating a third person mobile game, The camera is a separate gameobject of the player and follows the player around.

I calculate the movement direction of the player by looking at the Joysticks vertical and horizontal input. I Than use this direction to rotate the player in that direction.

The problem is that if i rotate to the right. and than i take my finger off the screen and rotate up. The character will reset to the state he was in prior to rotating right. What i want is for the the player to be able to rotate into any direction and walk forward into that direction. Its really hard to explain so i made a gif. Giphy link of Movement Notice how when i hold the joystick back, the player goes the other way.

Here is the code for controlling it

 // ===== INPUT
        private void LateUpdate()
         {
              MovementDirection = (new Vector3(variableJoystick.Horizontal, 0, variableJoystick.Vertical));
         }
 
 
 //ROTATION ========
        else if (MovementDirection != Vector3.zero & Dashing == false) {
                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(MovementDirection.normalized), 0.2f);
                 StartRotation = transform.rotation;
             }
 
 
 
 //MOVEMENT ========
             if (!_controller.isGrounded)
             {
                 MovementDirection.y -= Gravity * Time.deltaTime;
             }
             if (!Dashing)
             {
                 _controller.Move(MovementDirection.normalized * Time.deltaTime * speed);
             }
 


I have tried putting the player into another gameobject and rotating that gameobject aswell, but that didnt work so i thought he must be moving in world space so i tried local space and still did not work.

I know what the problem is, its cuz the horizontal and vertical axis always just point toward one direction. and back to zero. So i have to somehow rotate the axis. but no idea how.

Comment
Add comment
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

1 Reply

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

Answer by Bieere · Aug 11, 2019 at 04:59 PM

The issue is in your movement & Rotation code. The input that you're getting is correct but you need to separate the rotation and movement solving.


Your X value from Movement direction should rotate your character on its respective Vector3.up axis. Theoretically, the values for MovementDirection.x will fall between -1,1 so you can use that like so:

 //Rotation Code. You dont have to use Eulers, just using for simplicity
 Vector3 rotation = transform.localEulerAngles;
 rotation += MovementDirection.x * rotationSpeed * Time.deltaTime;
 transform.localEulerAngles = rotation;


 //Movement code, make sure you use your forward vector after rotating your character
 Vector3 position = transform.position;
 position += MovementDirection.y * transform.forward.normalized * speed * Time.deltaTime;


Your movement will use something similar, where you have your y value that again will fall between -1,1.

The important thing to remember is to set your rotation, then you can make use of the normalized forward vector to send your character in the direction that you've rotated them in.

Comment
Add comment · 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

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

255 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How can I fix my left and right movement which is currently affected by Cinemachine? 0 Answers

Movement Object 0 Answers

Mobile MOBA AOE spell aiming. 0 Answers

[VR] CameraRig and Neck 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