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 $$anonymous$$ · Mar 05, 2013 at 11:00 AM · c#inputquaternionangle

Lean main camera depending on input Axis (Horizontal)

I'm trying to do a simple leaning function for my character. Is an FPS game, what I'm trying to achieve is a small leaning of the main camera when moving on the horizontal axis to simulate the head movement beign dragged. The script kinda works but it doesn't take in account the negative value of the axis iven, so it only lean in one direction.

NOTE: The leaning is achieved by just rotating the main camera z euler angle.

Here is the script:

 using UnityEngine;
 
 [AddComponentMenu("Player Scripts/Player Head Track")]
 public class PlayerHeadTrack : MonoBehaviour {
     
     public GameObject mainCam;
     
     public float leanAngle = 5f;
     public float leanSpeed = 10f;
     
     float curAngle;
     float targetAngle;
     float angle;
     
     Quaternion rotAngle;
 
     // Lean main camera when moving horizontally
     public void LeanCamera (float axis) {
         curAngle = mainCam.transform.localEulerAngles.z;
         
         targetAngle = leanAngle - axis;
         
         if ( axis == 0.0f ) targetAngle = 0.0f;
         
         angle = Mathf.Lerp( curAngle, targetAngle, leanSpeed * Time.deltaTime );
         
         rotAngle = Quaternion.Euler( mainCam.transform.localEulerAngles.x, mainCam.transform.localEulerAngles.y, angle );
         mainCam.transform.localRotation = rotAngle;
     
     }
 }

To calculate the angle from my player controller script I just call the function LeanCamera this way:

When player is moving

 playerHt.LeanCamera(Input.GetAxis("Horizontal"));

When doing nothing

 playerHt.LeanCamera(0.0f);

How can I get both positive/negative values of the horizontal axis to work? I know they should be there no matter what, maybe is a problem on the LeanCamera script itself? Thanks in advance.

Comment
Add comment · Show 2
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 robertbu · Mar 09, 2013 at 06:38 AM 0
Share

I'm confused with your code, but what is actually doing and what you want it to do. Why are you calling LeanCamera() with the axix? Do you want it to lean more/faster if the movement is fast, or something else? If you are doing what I think you are trying to do, maybe this line:

 targetAngle = leanAngle - axis;

Should be something like:

 targetAngle = axis * leanAngle;

If you explain more, I might have some better ways for you to lean.

avatar image $$anonymous$$ · Mar 09, 2013 at 10:14 AM 0
Share

I don't want to add to the angle, I want to only lean toward a direction depending where the player is moving, only on the horizontal axis, so for example you strafe right the camera lean toward right with a fixed angle: http://www.youtube.com/watch?v=$$anonymous$$4On9P_2__c something like Quake.

$$anonymous$$aybe the problem here is another I guess Euler angle are only from 0 to 360 and doesn't have a negative angle?

1 Reply

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

Answer by Kleptomaniac · Mar 09, 2013 at 12:40 PM

Hey,

Having read your last comment, I believe I understand what you want now.

Try using Quaternion.Lerp:

 float maxRot = 45.0f; //Max neg and pos strafe rotation value
 float rate = 2.0f; //Rate of change for Lerp
 
 mainCam.transform.rotation = Quaternion.Lerp(mainCam.transform.rotation, Quaternion.Euler(axis * maxRot, mainCam.transform.rotation.y, mainCam.transform.rotation.z), Time.deltaTime * rate);

Hopefully that should work. Haven't tested though. :)

Klep

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 $$anonymous$$ · Mar 09, 2013 at 08:08 PM 1
Share

Thats it, it works, only things i had to change were the rotation to localRotation and the axis calculation to the Z of the euler. Thank you.

avatar image Kleptomaniac · Mar 09, 2013 at 10:33 PM 0
Share

Awesome dude, glad I could help! :)

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

How good is my "Quaternion" code efficiency? 1 Answer

Distribute terrain in zones 3 Answers

object is not rotating from right direction 0 Answers

How do you convert Input.GetAxis to rotation in degrees? 1 Answer

Location in relation to other objects rotation 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