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 Tomas_D · May 09, 2014 at 04:41 AM · camerarotation

Player Rotation relitive to active camera

Hello, I'm trying to make a silent hill/RE style game without take controls. I have it to were the player movement axis adjust based on the activeCamera object's transform. But I'm having trouble getting the rotation of the player graphics to work

Player Movement script:

 var moveSpeed : float = 10;
 var activeCamera : GameObject;
 
 function Update () 
 {
     var vert : float = Input.GetAxis("Vertical");
     var horz : float = Input.GetAxis("Horizontal");
     
     transform.eulerAngles.z = 0;
     transform.eulerAngles.x = 0;
     
     rigidbody.AddForce(activeCamera.transform.right*horz*moveSpeed);
     rigidbody.AddForce(activeCamera.transform.forward*vert*moveSpeed);
     
     
 }

This is what I have so far for the player graphics rotation. I just don't know where/how to implement the activeCamera's transform so that when I press down on the joypad the playergraphic rotates relative to it.

 var speed : float = 1.0;
 var playergraphic : Transform;
 var activeCamera : GameObject;
 
 function Update () 
 {
     transform.eulerAngles.z = 0;
     transform.eulerAngles.x = 0;
     transform.eulerAngles.y = 0;
     
     MoveRotation();
 }
 
 function MoveRotation ()
 {
     var vert : float = Input.GetAxis("Vertical");
     var horz : float = Input.GetAxis("Horizontal");
     var moveDirection = new Vector3(horz, 0, vert);
     
     if(moveDirection != Vector3.zero)
     {
         var newRotation = Quaternion.LookRotation(moveDirection);
         playergraphic.transform.rotation = Quaternion.Slerp (playergraphic.transform.rotation, newRotation, Time.deltaTime * speed);
         
     }
     
 }
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
0
Best Answer

Answer by Tomas_D · May 10, 2014 at 05:14 AM

Found the answer in this thread here: unity Answers forum

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
0

Answer by Mr.Hal · May 10, 2014 at 05:44 AM

Tell me if that works, I'm pretty sure this is how the Silent Hill team would do it. You might need to flip "Quaternion.LookRotation(playergraphic.position - activeCamera.transform.position)" to "Quaternion.LookRotation(activeCamera.transform.position - playergraphic.position)", if the direction is backwards.

 var speed : float = 1.0;
 var playergraphic : Transform;
 var activeCamera : GameObject;
  
 function Update () 
 {
     //Slerps the rotation of the camera to point at the player via a calculated direction
     activeCamera.transform.rotation = Quaternion.Slerp (activeCamera.transform.rotation, Quaternion.LookRotation(playergraphic.position - activeCamera.transform.position), Time.deltaTime * speed);
 }
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 Tomas_D · May 12, 2014 at 08:19 PM 0
Share

No man if I put it in exactly like you wrote it my player character sprouts three different axis and the graphic wigs out and flickers. When I flip it the player.graphic looks fine but the player control is revised. I managed to put this together and it works ok, but if activeCamera's y rotation is in the negative the player graphics start wigging out and sprouting three axis.

 var speed : float = 1.0;
 var playergraphic : Transform;
 var activeCamera : GameObject;
 
 function Update () 
 {
     transform.eulerAngles.z = 0;
     transform.eulerAngles.x = 0;
     
     
     $$anonymous$$oveRotation();
 }
 
 function $$anonymous$$oveRotation ()
 {
     var vert : float = Input.GetAxis("Vertical");
     var horz : float = Input.GetAxis("Horizontal");
     var moveDirection = new Vector3(horz, 0, vert);
     
     var camDir : Vector3 = activeCamera.transform.rotation * moveDirection;
     
     var objectDir : Vector3 = playergraphic.transform.position + camDir;
     
     
     
     if(moveDirection != Vector3.zero)
     {
         var newRotation = Quaternion.LookRotation(objectDir - playergraphic.transform.position);
         
         playergraphic.transform.rotation = Quaternion.Slerp (playergraphic.transform.rotation, newRotation, Time.deltaTime * speed);
         
     }
     
 }
avatar image Tomas_D · May 12, 2014 at 08:26 PM 0
Share

lol nvm about the negative rotation I'm just stupid. I was putting in -90 to get a rotation when I could of just put 270. The script I posted above works just remember to use positive numbers.

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

21 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

Related Questions

How to force camera rotation? 1 Answer

The solution with camera rotation which is used in most strategic games 1 Answer

Rotate camera 1 Answer

Smooth Follow Camera Rotate on Z-Axis? 2 Answers

Rotating gameobject after mouselook 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