Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Mrroundtree22 · Feb 04, 2016 at 05:17 PM · scripting problemfpsbeginnerscriptingbasicscontrols

First person shooter : Leaning!

I am a beginner with very limited knowledge when it comes to coding and scripting. I am currently working on a first person shooter and in it I wish to implement a "leaning system" similar to that found in the early Thief games.

Is there a simple script that I could add to the default FPSController that comes with Unity5 to achive this?

Here's a visual representation of what I mean (Sorry for the crap quality) : https://media.giphy.com/media/3o7ZeJogUUp3hhZpOU/giphy.gif

Again I would like to stress that I am beginner so please try and explain this to me in layman's terms ergo as simple as you possibly can.

Thanks.

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
2
Best Answer

Answer by Jessespike · Feb 04, 2016 at 08:29 PM

You can rotate the camera around a pivot to simulate a leaning effect. Assuming you're using the standard FPSController, add a pivot to the Controller and parent the camera to it. Like this:

  • FPSController

    • Pivot

      • FirstPersonController

Place the script on the camera (FirstPersonController), and set up the pivot in the inspector

 public class LeanBehaviour : MonoBehaviour {
     
     public Transform _Pivot;
 
     public float speed = 100f;
     public float maxAngle = 20f;
 
     float curAngle = 0f;
 
     // Use this for initialization
     void Awake () {
         if (_Pivot == null && transform.parent != null) _Pivot = transform.parent;
     }
     
     // Update is called once per frame
     void Update () {
 
         // lean left
         if (Input.GetKey(KeyCode.Q))
         {
             curAngle = Mathf.MoveTowardsAngle(curAngle, maxAngle, speed * Time.deltaTime);
         }
         // lean right
         else if (Input.GetKey(KeyCode.E))
         {
             curAngle = Mathf.MoveTowardsAngle(curAngle, -maxAngle, speed * Time.deltaTime);
         }
         // reset lean
         else
         {
             curAngle = Mathf.MoveTowardsAngle(curAngle, 0f, speed * Time.deltaTime);
         }
 
         _Pivot.transform.localRotation = Quaternion.AngleAxis(curAngle, Vector3.forward);
     }
 
 }
 
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 Mrroundtree22 · Feb 04, 2016 at 09:46 PM 0
Share

Thank you very much ill try it out right now.

avatar image Mrroundtree22 · Feb 04, 2016 at 10:02 PM 1
Share

Yes, it works- Thank you!

avatar image SonEvil · Oct 17, 2017 at 04:43 PM 0
Share

awesome ! thank you

avatar image
0

Answer by leejewitt · Jun 28, 2021 at 05:31 PM

hi how would if get it to stop at the angle it hits a wall as i can lean straight through walls?

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

49 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 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

Jump Animation Script and colliders not working properly 1 Answer

How to increase the time between updates? 1 Answer

Animations Bad Scripting | Controller Script Buggy | Collider Problem | Beginner Here! 0 Answers

BasicFPS controller "Can not be loaded" 0 Answers

why the Timer isn't working 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