Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Alverik · Sep 22, 2016 at 12:52 AM · c#mergecrouchheadbobboth

FP Camera how to crouch and headbob at the same time

Hi, I wanted to know how properly use both headbob and crouching in FP camera at the same time. I'm somewhat new to unity and programming and I've never done any first person before, so I'm having a little trouble. this is what I have right now:

 public class FpsCrouch : MonoBehaviour
     {
         public float CrouchHeight = .9f;
         public float CrouchSmoothTime = 0.3f;
 
         protected Vector3 _baseCamHeight;
         private Vector3 _crouchCamHeight;
 
         protected Vector3 _currentVelocity;
         private float _currentVelocityB;
 
         public static bool ForceCrouch;
 
         private AC.Player _player;
         private CapsuleCollider _playerCollider;
         private float _playerWalkSpeed = 0f;
         private float _playerRunSpeed = 0f;
         private bool _speedWasChanged;
 
         private float _originalColliderSize;
         private float _targetColliderSize;
 
         private Vector3 _originalColliderCenter;
         private Vector3 _targetColliderCenter;
 
         public Vector3 TargetPosition;
 
         protected virtual void Awake()
         {
             // Assume starting height is standing height.
             _baseCamHeight = transform.localPosition;
 
             // Move that position down to find crouching position.
             _crouchCamHeight = transform.localPosition;
             _crouchCamHeight.y = CrouchHeight;
         }
 
         protected virtual void Start()
         {
 
             _player = GameObject.FindGameObjectWithTag("Player").GetComponent<AC.Player>();
             _playerWalkSpeed = _player.walkSpeedScale;
             _playerRunSpeed = _player.runSpeedScale;
 
             _playerCollider = _player.GetComponent<CapsuleCollider>();
 
             _originalColliderSize = _playerCollider.height;
             _originalColliderCenter = _playerCollider.center;
 
         }
 
         protected virtual void Update()
         {
             
 
             //if the player holds the crouch button
             //or the system enforces a crouch state, then crouch
             if (Input.GetButton("Crouch") || ForceCrouch)
             {
                 TargetPosition.y = _crouchCamHeight.y;
 
                 _targetColliderSize = _originalColliderSize / 1.5f;
                 _targetColliderCenter = _originalColliderCenter / 2;
 
                 //if the speed wasn't changed yet.
                 if (!_speedWasChanged)
                 {
                     //change it
                     _player.runSpeedScale = _playerWalkSpeed;
                     _speedWasChanged = true;
                 }
 
                 CrouchTransition(TargetPosition);
 
             }
             else
             {
                 TargetPosition.y = _baseCamHeight.y;
                 _targetColliderSize = _originalColliderSize;
                 _targetColliderCenter = _originalColliderCenter;
                 //if the speed was changed before. Reset it.
                 if (_speedWasChanged)
                 {
                     _player.runSpeedScale = _playerRunSpeed;
                     _speedWasChanged = false;
                 }
 
                 CrouchTransition(TargetPosition);
             }
         }
 
 
         protected void CrouchTransition(Vector3 targetPosition)
         {
 
             //transition the collider's position
             _playerCollider.height = Mathf.SmoothDamp(_playerCollider.height, _targetColliderSize,
                 ref _currentVelocityB,
                 CrouchSmoothTime);
 
             //transition the collider's center
             _playerCollider.center = Vector3.SmoothDamp(
                 _playerCollider.center,
                 _targetColliderCenter,
                 ref _currentVelocity,
                 CrouchSmoothTime);
 
             //transition the camera to the appropriate position.
             transform.localPosition = Vector3.SmoothDamp(
                 transform.localPosition,
                 targetPosition,
                 ref _currentVelocity,
                 CrouchSmoothTime
             );
 
         }
 
     }

and:

 public class HeadBob : FpsCrouch
     {
 
         //local position where your camera would rest when it's not bobbing.
         private Vector3 restPosition;
         //smooths out the transition from moving to not moving.
         public float transitionSpeed = 20f;
         //how quickly the player's head bobs.
         public float bobSpeed = 4.8f;
         //how dramatic the bob is. Increasing this in conjunction 
         //with bobSpeed gives a nice effect for sprinting.
         public float bobAmount = 0.1f;
 
 
         //initialized as this value because this is where sin = 1. 
         //So, this will make the camera always start at the crest of the sin wave, 
         //simulating someone picking up their foot and starting to walk--you experience 
         //a bob upwards when you start walking as your foot pushes off the ground, 
         //the left and right bobs come as you walk.
         float timer = Mathf.PI / 2;
         Vector3 camPos;
 
         protected override void Awake()
         {
             restPosition = new Vector3(0, transform.localPosition.y, 0);
             camPos = transform.localPosition;
             base.Awake();
         }
         
         protected override void Update()
         {
             base.Update();
             if (!(Math.Abs(transform.localPosition.y - _baseCamHeight.y) < 0.03f) && (Input.GetButton("Crouch") || ForceCrouch)) return;
 
             if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) //moving
             {
                 timer += bobSpeed * Time.deltaTime;
 
                 //use the timer value to set the position
 
                 //if you're not crouching
                 if ((Input.GetButton("Crouch") || ForceCrouch))
                 {
                     return;
                 }
 
                 Vector3 newPosition = new Vector3(Mathf.Cos(timer) * bobAmount,
                     restPosition.y + Mathf.Abs((Mathf.Sin(timer) * bobAmount)), restPosition.z); //abs val of y for a parabolic path
                 transform.localPosition = newPosition;
 
                 Debug.Log("I'm bobbing!");
             }
             else
             {
                 timer = Mathf.PI / 2; //reinitialize
 
                 if ((Input.GetButton("Crouch") || ForceCrouch))
                 {
                     return;
                 }
 
                 Vector3 newPosition = new Vector3(Mathf.Lerp(camPos.x, restPosition.x, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.y, restPosition.y, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.z, restPosition.z, transitionSpeed * Time.deltaTime));
                 transform.localPosition = newPosition;
 
             }
 
             if (timer > Mathf.PI * 2) //completed a full cycle on the unit circle. Reset to 0 to avoid bloated values.
                 timer = 0;
         }
         
     }

I put this together using several examples I found on the forums as a base, then modified some stuff to my taste. But, at first they only worked by themselves. One always blocked the other, so I tried to make some changes to allow one to know if crouching was taking place, but the problem is, this way headbobbing only works while not crouching and also there's a sudden "jump" in the camera when you release the crouch button while you are walking or running...

So, how would I go about merging both camera smoothdamp operations? I do really love the parabollic headbob sway, but I'm a little confused about how I'd use it while crouching too.

Anyway, any advice will be greatly apprectiated.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Character moves slightly while crouching? 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

My enemies merge when they come towards me 4 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