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 FuryFight3r · Jan 26, 2019 at 04:48 AM · camerarotate3rd person controller

Move Character in Relative Direction to Camera

Hi guys, im struggling with moving the character in relation to which way the camera is facing. So what's happening is the world has a North, East, South and East, (+ & - Z and X) and when I press 'W' to move forward the character will always move 'North' no matter the rotation of the camera, how do i make it so my character will always go in the direction of the camera when 'W' is Pressed?

This is my Movement piece:

     public void moveForward()
     {
         Vector3 temp = _XForm_Parent.position;
         if (isRunning)
         {
             temp.z += movementSpeed * runningSpeed;
         }
         else
         {
             temp.z += movementSpeed;
         }
         _XForm_Parent.position = temp;
     }

And this is my Orbiting piece:

     void LateUpdate()
     {
         if (!CameraDisabled)
         {
             if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
             {
                 _LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
                 _LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
             }
 
             _LocalRotation.y = Mathf.Clamp(_LocalRotation.y, 5f, 90f);
 
             if (CanScroll && Input.GetAxis("Mouse ScrollWheel") != 0f)
             {
                 float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity;
 
                 ScrollAmount *= (_CameraDistance * 0.3f);
 
                 _CameraDistance += ScrollAmount * -1f;
                 _CameraDistance = Mathf.Clamp(_CameraDistance, 1.5f, 100f);
             }
         }
 
         if (Input.GetKey(KeyCode.Mouse3))
         {
             if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
             {
                 _LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
                 _LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
             }
 
             _LocalRotation.y = Mathf.Clamp(_LocalRotation.y, 5f, 90f);
 
             if (CanScroll && Input.GetAxis("Mouse ScrollWheel") != 0f)
             {
                 float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity;
 
                 ScrollAmount *= (_CameraDistance * 0.3f);
 
                 _CameraDistance += ScrollAmount * -1f;
                 _CameraDistance = Mathf.Clamp(_CameraDistance, 1.5f, 100f);
             }
         }
 
         Quaternion QT = Quaternion.Euler(_LocalRotation.y, _LocalRotation.x, 0);
         _XForm_Parent.rotation = Quaternion.Lerp(_XForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);
         if (_XForm_Camera.localPosition.z != _CameraDistance * -1f) ;
         {
             _XForm_Camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(_XForm_Camera.localPosition.z, _CameraDistance * -1f, Time.deltaTime * ScrollDampening));
         }
     }


My main question is how do I always make the character move 'Forward' not North, but in relation as to the cameras direction.

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
1
Best Answer

Answer by FuryFight3r · Jan 27, 2019 at 11:10 AM

I found the fix, whilst experimenting earlier, I hadn't put .parent. in to move the camera pivot not the camera.

I changed this:

          public void moveForward()
          {
              Vector3 temp = _XForm_Parent.position;
              if (isRunning)
              {
                  temp.z += movementSpeed * runningSpeed;
              }
              else
              {
                  temp.z += movementSpeed;
              }
              _XForm_Parent.position = temp;
          }

To this:

     public void moveForward()
     {
         Vector3 temp = _XForm_Parent.position;
         if (isRunning)
         {
             temp += transform.parent.forward * Time.deltaTime * runningSpeed;
             temp.y = 1;
         }
         else
         {
             temp += transform.parent.forward * Time.deltaTime * movementSpeed;
             temp.y = 1;
         }
         _XForm_Parent.position = temp;
     }

and did the same for left right and backward accordingly.

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
avatar image
1

Answer by gunboldb · Jan 27, 2019 at 07:40 AM

I am just taking a guess here. You could take the appropriate rotation angle of the camera and equal it to the rotation of your character or something to that nature.

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 FuryFight3r · Jan 27, 2019 at 08:24 AM 0
Share

Hi thanks for your answer, i did experiment with this, but the movement is being added and subtracted with a vector 3, meaning it only goes by world space, not transform space, i still have this feature implemented, as the object that is rotating to move the camera by mouse, is the same object being moved by the vector 3 with W,A,S,D, how can i convert it to go forward to the transforms position not world position?

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

160 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

Related Questions

Move character and rotate camera on same joystick 0 Answers

Move character in direction its facing 1 Answer

Camera rotate - point and click game 1 Answer

Can the Camera Smoothly rotate AFTER a button is pressed? (Javascript) 1 Answer

Camera RotateAround not rotating around center like a planet 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