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
2
Question by muzza74 · Jul 21, 2013 at 03:14 PM · camerarotationmovementrelative

Camera Relative Movement

I'm trying to write my own controller for a third person character and I'm having some trouble making the character move relative to a camera i.e. pressing up will always make the character walk away from the camera, pressing right will always cause the character to move right relative to the camera.

Here's my current code for setting the rotation of the character:

 private void updateRotation(){
         Transform cameraTransform = theCamera.transform;
         // forward of the camera on the x-z plane
         Vector3 cameraForward = theCamera.transform.TransformDirection(Vector3.forward);
         cameraForward.y = 0f;
         cameraForward = cameraForward.normalized;
         
         Vector3 cameraRight = new Vector3(cameraForward.z, 0.0f, -cameraForward.x);
         
         float vert = Input.GetAxisRaw("Vertical");
         float hor = Input.GetAxisRaw("Horizontal");
         
         Vector3 tempTarget = hor * cameraRight + vert * cameraForward;
         if (tempTarget!= Vector3.zero){
             theController.moveDirection = Vector3.RotateTowards(theController.moveDirection, tempTarget, theController.rotSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
             theController.moveDirection = theController.moveDirection.normalized;
             theController.transform.rotation = Quaternion.LookRotation(theController.moveDirection);
         }
         
     }

And here is how my controller moves the character forward:

 private void moveCharacter(){
         transform.Translate(transform.forward*moveSpeed*Time.deltaTime);
 }

Currently the character moves relative to the camera at first, then seems to "lose it's axis" so to speak. Any help would be appreciated.

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

2 Replies

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

Answer by robertbu · Jul 21, 2013 at 04:06 PM

I believe the root of the issue is that Transform.Translate take a Local not a World coordinate. The character movement code should be:

 transform.Translate(Vector3.forward*moveSpeed*Time.deltaTime);

or you could do:

 transform.position += transform.forward*moveSpeed*Time.deltaTime;

A few other comments:

  • Lines 4-6 can be replaced by 'theCamera.transform.forward' (which is normalized).

  • Your cameraRight code does not calculate the right of the camera...it is an angle right. If you want the actual right (which I think you do) you can just use 'theCamera.transform.right'.

  • Why GetAxisRaw() instead of GetAxis()?

So you can reduce lines 1 - 12 to:

 Vector3 tempTarget = Input.GetAxis("Vertical") * theCamera.transform.forward + Input.GetAxis("Horizontal) * theCamera.transform.right;
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 muzza74 · Jul 21, 2013 at 04:19 PM 0
Share

I thought the problem might have been something not being in world space, but I couldn't figure out what. I was mainly using code from a tutorial to implement this, hence the GetAxisRaw() and the unnecessary code. Thanks for your solution, it worked perfectly! Edit: The reduction you suggested results in a vector with a non-zero y-axis, it needs to be zeroed first i.e.

 Vector3 tempTarget = Input.GetAxis("Vertical")* theCamera.transform.forward + Input.GetAxis("Horizontal") * theCamera.transform.right;
 tempTarget.y = 0f;

This way the character only rotates around the y-axis i.e. on an x-z plane

avatar image
0

Answer by meat5000 · May 26, 2014 at 05:27 PM

I know its old but I wrote one recently so I'll write some tips while its fresh in my mind.

I wrote for Android, so a visual stick is present on screen.

I made animations for Walk/Run forward, backward and turn left, turn right.

Stick.Y = Walk

Stick.X = Turn.

So normally, up on stick makes character walk forward no matter his facing direction. This is your link between Camera and Joystick. Up on stick and Forward on Camera correspond to 0 degrees.

The formula goes

Jr = -P + Jp

(Jr = Joystick Rotation Required, P = Player rotation from camera, Jp = Joystick Press Rotation)

So, find clockwise rotation of Player (P) with respect to camera forward. Make it Negative. Use AngleAxis to create a rotation of this angle around Vector3.up. Multiply the Quaternion by your input stick vector.

Note, mapping Stick x,y to Vector3 x,z helps with simplistic Vector2 rotation. Also one will require the use of Vector3.cross to find Angles over 180 degrees.

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

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

Relative Movement Problem 1 Answer

Global movement 0 Answers

Calculate vector exactly between 2 vectors 4 Answers

Problem with camera position 3 Answers

In-Air movement relative to camera 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