• 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
Question by Althaen · Nov 16, 2012 at 04:34 AM · c#cameratransformmouserotate

Make player face same direction as camera.

I currently have a camera that can be rotated around the player by holding shift and moving the mouse. The only problem is that now I want the player to move in a direction relative to the direction camera is facing slowly and to rotate to face the same direction as the camera.

I hope that was clear enough to understand :|

Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Althaen · Nov 18, 2012 at 01:30 AM

Just decided to rotate the player with the camera attached to it. Works fine so far.

Comment

People who like this

0 Show 0 · 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

Answer by sparkzbarca · Nov 16, 2012 at 04:51 AM

so you want to rotate towards something?

How about the RotateTowards function :)

if you dont want to set a max speed you could just use slerp feed it the from and the to and it'll make it happen

transform.rotation = quanternion.slerp(quanternion from, quanternion to);

now you still need to get the to and the from and have them in quanternion form.

we know we want to rotate from the front of the player to the front of the camera.

actually since you want the the front to face the front you really just want the players rotation to match the cameras. So we can shortcut getting vectors and turning them into quanternions and go straight to

transform.rotation = quanternion.slerp(player.transform.rotation, camera.transform.rotation);

you want to move in a direction relative to the camera.

Well direction is a vector. and the vectors relative to a gameobject are stored in its transform.

camera.transform.forward is a vector which points from the center of the camera directly forward (it can be thought of as passing through the point in the middle of the screen.

-camera.transform.forward is the inverse. A negative sign reverses the direction basically. forward becomes backward.

with camera.transform.right,camera.transform.up,camera.transform.forward and the negative sign we can get all 6 directions of movement and we can move an object in those directions.

if we'd like we can normalize those vectors. A normalized vector is basically the slope of the line.

its as if the center of the object is 0,0,0 and not wherever it is in world space.

if we just add that vector to an existing position we move in the same direction as that line. except with respect to the current objects position.

So we could do

player.transform.position += camera.transform.forward.normalized;

and bam its moving parallel to the camera.

there are other ways as well you dont have to normalize you can you transformdirection but the end result is the same. Look up transformdirection in unity script reference for more details on that.

Mark as answered.

Comment

People who like this

0 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 Althaen · Nov 17, 2012 at 03:40 AM 0
Share

How can I lock the characters rotation to up? Because my camera sits at an angle and now my player faces to floor and the camera spazzes out.

avatar image

Answer by Coreyf716 · Nov 18, 2012 at 01:31 AM

I just use the horizontal mouse look in the player controller script and a seperate vertical mouse look script attached to the main camera.

Comment

People who like this

0 Show 0 · 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

Answer by GarbageElitist · Nov 18, 2012 at 04:30 AM

3DBuzz actually has a third person controller video tutorial series that goes into this a bit.

http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=6d5630dac2c391b9e563c4693d44d7cc

It's sort of long, but since you seem fairly experienced, you could always skip to the parts where they talk about camera. Their player snaps to camera view, much like if you held down right click in an MMO.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Moving Camera on player stop then move and rotate to separate point? 0 Answers

PauseMenu - Camera moves in pausemenu 0 Answers

Find the Y rotation of a cylinderTarget tracked with Vuforia 1 Answer

Rotating a camera 90 degrees around an object 1 Answer

Movement with camera and rotation of objects 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