• 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 Fury-Fight3r · 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 Fury-Fight3r · 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 Fury-Fight3r · 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to keep camera at certain level while moving 1 Answer

Float the camera back to his position 1 Answer

Logical Camera errors... 1 Answer

How to know if rotating clockwise or counterclockwise 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges