Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ygg1g · Aug 21, 2017 at 04:32 AM · camerarotationmovementvector3transformdirection

Movement relative to Camera and XZ Plane

I have a 3rd person controller set up based on the 3DBuzz's tutorials. I want to move in the direction the camera is facing but only using the input on the XZ plane. And so far i've come up with this:

     void ProcessMotion()
     {
         // Transform MoveVector to World Space
         MoveVector = Camera.main.transform.TransformDirection(MoveVector);
         MoveVector = new Vector3(MoveVector.x, 0.0f, MoveVector.z);
 
         // Normalize MoveVector if magnitude > 1
         if (MoveVector.magnitude > 1)
             MoveVector = Vector3.Normalize(MoveVector);
                     
         // Move the Character in World Space
         TPS_Controller.CharacterController.Move(MoveVector * moveSpeed * Time.deltaTime);        
     }

The result of this is when i move, if i look up or down (rotate the camera on X axis), the character slows down because of the Z value received from the TransformDirection:

alt text

untitled.png (2.2 kB)
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 haruna9x · Aug 21, 2017 at 05:52 AM 0
Share

So is your character following the camera? It should be the opposite.

1 Reply

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

Answer by Ygg1g · Sep 25, 2017 at 03:08 AM

I found a workaround for this issue, i don't know if this is the most effective solution but it works 100%. I created an empty game object which stores ONLY the Y rotation value of the camera. So the direction of this new game object is only on the XZ plane.

Code:

     void ProcessMotion()
     {
         // Transform MoveVector to World Space
         MoveVector = Pivot.Instance.transform.TransformDirection(MoveVector);
 
         // Normalize MoveVector if magnitude > 1
         if (MoveVector.magnitude > 1)
             MoveVector = Vector3.Normalize(MoveVector);
 
         // Move the Character in World Space
         TPS_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
     }

New GameObject code:

 public class Pivot : MonoBehaviour
 {
     public static Pivot Instance;
 
     void Awake ()
     {
         Instance = this;
     }
     
     void Update ()
     {
         transform.rotation = new Quaternion(0.0f, Camera.main.transform.rotation.y, transform.rotation.z, Camera.main.transform.rotation.w);
     }
 }
Comment
Add comment · Show 1 · 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 saxenasiddhant12 · Aug 07, 2021 at 04:53 PM 0
Share

This is exactly what I needed. Thanks a lot I was facing the same exact issue and this solution works perfectly.

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

145 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

Related Questions

How do I turn 1 objects rotation into another objects movement direction? 1 Answer

Smooth Attached Object movement 1 Answer

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

How do you make a gameobject rotate slowly to the direction of the mouseclick 0 Answers

How to smooth my camera (Vector3.Lerp) 0 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