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
1
Question by benandlucy · Jul 22, 2013 at 05:58 PM · camerarotationplayer movementy axis

How to make player move in the direction of where the camera is facing.(Y axis only)

I have a First Person game, where if you move the mouse, the camera rotates. I also have a script for when you use the WASD keys, the player moves accordingly. But when the camera rotates, the player still moves in the same direction. This is logical, but how can I make the player's movement's direction change with the camera's rotation on the Y axis. If you need any help understanding what's going on, just ask.

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 clunk47 · Aug 09, 2013 at 05:18 PM 0
Share

Please either accept the answer that has resolved your concern, let us know it has not been resolved, or close the question.

7 Replies

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

Answer by clunk47 · Jul 22, 2013 at 08:47 PM

 transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z); 

This will keep the player's local Y angle with the main camera as it rotates. Make sure the camera is tagged "MainCamera". Having only rotate the player itself on the Y axis will keep it from rotating sideways / back and forth if the camera looks around in different directions. Say your camera is high up, and moves lower, but still has to look at the player. This requires the camera to change angles on the x axis, which you don't want your player to do.

transform.localEulerAngles

Comment
Add comment · Show 2 · 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 nabarun1011 · Mar 30, 2017 at 05:33 PM 0
Share

everytime my player rotates with camera , the camera shakes . Why is that?

avatar image HaGGGames · Feb 03, 2019 at 07:45 PM 0
Share

This worked for me my player now rotates when i move my mouse left and right and the camera stays behind him ins$$anonymous$$d of rotating in a 360 degree movement around my player which I wanted but now I want the player to move in the direction of of the rotation and what is currently happening is I press W to walk forward he walks forward but when I rotate my mouse left or right the player will continue to walk forward in that same direction and not move forward in the direction I rotate any ideas on how can i fix this?

avatar image
5

Answer by rhys_vdw · Jul 25, 2013 at 03:16 AM

Although clunk47's answer is correct, I would suggest a different approach.

Child the camera to the player, and change your camera movement script to rotate the player when the mouse moves horizontally, and rotate the camera when the mouse moves vertically.

If that is not clear, post your code and I'll give you some tips.

Comment
Add comment · Show 3 · 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 MalachiteBR · Jul 26, 2013 at 08:52 PM 0
Share

Yeah, thats a very nice idea.

avatar image clunk47 · Jul 31, 2013 at 04:51 AM 1
Share

Sounds good, +1.

avatar image john316 · Nov 12, 2015 at 09:13 AM 0
Share

Hi sir im a beginner and im having the same trouble. I've parented the main camera already, what's next?

This is the code for player's movement:

   void FixedUpdate()
     {
         //Store input axes
         float lh = CrossPlatformInput$$anonymous$$anager.GetAxisRaw("Horizontal");
         float lv = CrossPlatformInput$$anonymous$$anager.GetAxisRaw("Vertical");
 
         $$anonymous$$ove(lh, lv);
 
         Animating(lh, lv);
     }
 
     void $$anonymous$$ove(float lh, float lv)
     {
         //$$anonymous$$ove the player
         movement.Set(lh, 0f, lv);
 
         movement = movement.normalized * speed * Time.deltaTime;
 
         playerRigidbody.$$anonymous$$ovePosition(transform.position + movement);
 
         if (lh != 0f || lv != 0f)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), turnSpeed * turnSmoothing * Time.deltaTime);
             transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
             targetRotation = transform.rotation;
         }
     }
avatar image
0

Answer by roojerry · Jul 22, 2013 at 07:05 PM

use camera.transform.forward

Comment
Add comment · Show 4 · 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 benandlucy · Jul 22, 2013 at 07:26 PM 0
Share

Um, I don't think you understand. I want the camera to o all the rotating, and the player to move. You see, if I press W, my player goes forward, and when my camera rotates, he still goes forward in the same direction he was going. I want him to go in the direction of the camera.

avatar image roojerry · Jul 22, 2013 at 07:27 PM 2
Share

so, move your player in the forward direction of the camera

avatar image benandlucy · Jul 22, 2013 at 07:32 PM 0
Share

How? Sorry I'm a begginner at unity.

avatar image roojerry · Jul 22, 2013 at 08:05 PM 0
Share

What code are you using to move your player forward at the moment?

avatar image
0

Answer by MalachiteBR · Jul 23, 2013 at 11:56 AM

As the previous answer stated, you need a script that do something like that:

 Update()
 {
   transform.forward = GameObject.Find("MainCamera").forward;
 }

This way your character will have its "forward" always pointing to camera forward.

If you like my answer, upvote it plz :)

Comment
Add comment · Show 4 · 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 clunk47 · Jul 25, 2013 at 03:05 AM 1
Share

Would be GameObject.Find("$$anonymous$$ainCamera").transform.forward if you want to use the transform of the camera.

avatar image clunk47 · Jul 25, 2013 at 03:05 AM 1
Share

You could also just use Camera.main.transform if you're referencing the $$anonymous$$ain Camera.

avatar image clunk47 · Jul 25, 2013 at 03:07 AM 1
Share

But if you make the player's transform.forward the same as the camera's forward, the player object will rotate on ALL the axes as the camera does. He wants Y axis only.

avatar image rhys_vdw · Jul 25, 2013 at 03:14 AM 1
Share

That line wont work, as Find returns a GameObject. You'll need to get its transform. A more succinct way of writing this would be:

 transform.forward = Camera.main.transform.forward;
avatar image
0

Answer by woody3d · Jul 29, 2014 at 01:18 AM

No one will ever give you the simple answer around here. Too busy trying to look big here of all places. LOL. Just make sure you have NO mono behavior scripts on you CAMERA in Inspector, Instead, go to the FirstPersonController Prefab (I assume this is what you are using) and add the component MouseLook Script under camera control. I think you ere adding the script to the camera which was sending your camera looking in a different direction without telling your 'controller' whats going on... Applying it to the top of your firstpersoncontroler hierarchy lets the whole chain benefit from the Mono behavior script.

I'm only 2 lessons into learning Unity but that worked for me! essentially, Make sure you have the behavior only on the controller not on the camera if it's linked!

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 benandlucy · Jul 29, 2014 at 01:49 AM 0
Share

Thanks for replying an answer, but take a look at the year this was posted haha. I'm a bit beyond this level of Unity.

  • 1
  • 2
  • ›

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

28 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

Related Questions

Player jitters when moving. 1 Answer

Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers

Rotation of player based on camera direction and joystick direction in 3D world 1 Answer

Camera Rotating when the player reach a specific position 0 Answers

Need help for player rotation and camera rotation 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