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 /
  • Help Room /
avatar image
0
Question by J0K3R_12QQ · Mar 11, 2018 at 09:22 AM · cameracontrollerthird-person

How can I make a Unreal Engine Example Project third preson player controller?

I have recently moved over from UE4 to Unity and I'm trying to learn how to make a nice third person controller. I am aiming for something similar to inFamous: Second Son or Batman: Arkham Knight kind of controller.
Basically I want the camera to orbit around the player, when the player is not moving (which I have already done) and if the user presses W, A, S or D, the character has to rotate towards the input (if the users presses D the player has to rotate to the right, if he presses A - to the left, W - forward and S - back) and then move forward.
Right now my problem is, that the player moves sideways when I press A or D instead of doing what I previously described.
Also it would be great if the player was moving using it's Rigidbody instead of Transform.
I'd really appreciate your help.

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 meat5000 · Mar 11, 2018 at 09:57 AM

You need to make your movement Camera Relative, in that regard, if you control system depends on camera facing direction. You just need to find the angle difference around y axis and compensate for it in the controls.

Comment
Add comment · Show 6 · 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 J0K3R_12QQ · Mar 11, 2018 at 10:09 AM 0
Share

Can you show me how it looks in code?

avatar image meat5000 ♦ J0K3R_12QQ · Mar 11, 2018 at 10:14 AM 0
Share

I could, but I'd encourage you to have a go first.

~

https://docs.unity3d.com/$$anonymous$$anual/VectorCookbook.html

~

https://www.google.co.uk/search?q=unity+camera+relative&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=xgGlWr7BHaaLtgeV_LPoDg

~

I'll hint that problems like this are best drawn up on paper.

avatar image J0K3R_12QQ meat5000 ♦ · Mar 11, 2018 at 11:20 AM 1
Share

Ok, so I followed some tutorials and read some documentation and came up with this code:

         private void Update()
         {
             Calculate$$anonymous$$ovement();
         }
     
         private void FixedUpdate()
         {
             playerRigidbody.$$anonymous$$oveRotation(Quaternion.Lerp(transform.rotation, rotation, .25f));
             playerRigidbody.$$anonymous$$ovePosition(playerRigidbody.position + movement * Time.fixedDeltaTime);
         }
     
         private void Calculate$$anonymous$$ovement()
         {
             float x = Input.GetAxis("Horizontal");
             float z = Input.GetAxis("Vertical");
     
             Vector3 input = new Vector3(x, 0f, z);
             Vector3 camForward = playerCamera.transform.forward;
             camForward.y = 0f;
     
             Quaternion relative = Quaternion.FromToRotation(Vector3.forward, camForward);
             Vector3 look = relative * input;
     
             if (input != Vector3.zero)
             {
                 rotation = Quaternion.LookRotation(look);
                 movement = transform.forward * speed;
             }
             else
                 movement = Vector3.zero;
         }

It works, but I want to hear your opinion on it's efficiency. What do you think?

Show more comments

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

126 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

Related Questions

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

Connect controls to Camera angle 0 Answers

Xbox one controller spinning 0 Answers

Unity3D accelerometer camera rotation realistic controls 0 Answers

FPSController Prefab - How can i change the Z position of my camera? 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