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
0
Question by Lioki · Mar 17, 2013 at 07:55 AM · 3rd person controllercamera controls

How to get character to face camera direction

I am using the MouseOrbitImproved script for camera control and animator controller with the botcontrol script from the Mecanim tutorial. What I need to have is for the character to always move in the direction the camera is facing when applying movement controls or pressing a mouse button. Right now it continues to move in the direction it's currently facing no matter what direction the camera is.

Comment
Add comment · Show 6
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 Lockstep · Mar 17, 2013 at 11:01 AM 0
Share

Read these docs

avatar image Lioki · Mar 17, 2013 at 06:59 PM 0
Share

I have. The character will rotate with the camera, but I want the orbital camera/zoom and no character movement until an input is made. No matter how I try to script it I can't get the inputs to have any effect. When I have a bit I'll post up my script.

avatar image nsxdavid · Mar 17, 2013 at 07:05 PM 0
Share

Show the code you are having trouble with, it's much easier for someone to help that way than a broad general question like that.

avatar image Lioki · Mar 17, 2013 at 09:20 PM 0
Share

var target : GameObject; var followingDistance : float = 25.0;

 private var rotationAngle : float = 0.0;
 private var cameraY : float = 20.0;
  
 var cameraSpeed = 3.0;
 var targetRotationSpeed = 1.5;
 
    
   function LateUpdate () {
        
  
 //$$anonymous$$ake sure that the object is centered in camera and is always on the same distance
 var mouseY = -5.0 * Input.GetAxis ("$$anonymous$$ouse Y");
 cameraY += mouseY;
  
 //v = target - camera
 var targetCameraPosition : Vector3 = -(target.transform.forward - transform.position).normalized * followingDistance + Vector3.up * cameraY;
  
 //Rotate camera on plane xz
 var mouseX = -5.0 * Input.GetAxis ("$$anonymous$$ouse X");
 rotationAngle += mouseX;
  
 var rotationQuaternion1 : Quaternion = Quaternion.AngleAxis(rotationAngle, Vector3.up);
 targetCameraPosition = rotationQuaternion1 * targetCameraPosition;
 targetCameraPosition += target.transform.position;
 targetCameraPosition = Vector3.Slerp(transform.position, targetCameraPosition, Time.deltaTime * cameraSpeed);
 transform.position = targetCameraPosition;
  
 //$$anonymous$$ake camera look at the object
 var rotationQuaternion2 : Quaternion = Quaternion.LookRotation( (target.transform.position - transform.position).normalized );
 transform.rotation = Quaternion.Slerp(transform.rotation, rotationQuaternion2, Time.deltaTime * cameraSpeed);
  
 //
 //Rotate target accordingly
  
 //$$anonymous$$ake sure that the target will rotate only on xz plane
 var xzRotationVector = (target.transform.position - transform.position).normalized;
 xzRotationVector.y = 0.0;
  
 var targetQuaternion : Quaternion = Quaternion.LookRotation( xzRotationVector );
 target.transform.rotation = Quaternion.Slerp(target.transform.rotation, targetQuaternion, Time.deltaTime * targetRotationSpeed);
 }
avatar image Lioki · Mar 17, 2013 at 09:22 PM 0
Share

This is the camera control script that will turn the player to face the same direction as the camera is facing, but I don't want that to happen until a Get$$anonymous$$eyDown is made. I might want it to snap direction ins$$anonymous$$d of using slerp too.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Lioki · Mar 19, 2013 at 12:50 AM

// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down or Horizontal is pressed.

     if(Input.GetMouseButton(0) || Input.GetButton("Vertical")) {
         transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
     } else {
         transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
     }

Fixed it. Added this to my control script, Thanks for everyones help.

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

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

connecting unity camera with realworld camera 0 Answers

Stop a player controller from duplicating. 1 Answer

transform.rotation and localRotation automatically reset after rotating. 1 Answer

Question about Unity's standard Assets ? 1 Answer

How do I attach multiple animations at a time to a character? And how to I make them trigger when a user presses keys? 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