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 /
  • Help Room /
avatar image
0
Question by FusionPwN · Sep 09, 2015 at 02:44 PM · c#smoothdampcrouch

Mathf.SmoothDamp problem

Hello, I'm kind of new to unity, I mean I know the basics, and I'm learning C# so sorry if this is a stupid question... I've been searching on google how to solve this and I got this far.

So I'm using Unity's standard asset FPSController, I've added the ability to crouch then I wanted to smooth the transform of the Y position on the camera, I've explored the Mathf functions, lerp, smoothdamp, smoothangle and I understand what they do. I have implemented smoothdamp on my code but I'm not getting my expected result.

Inicially the camera's Y position is 1.5 when I press the crouch button the value is changed to half (0.75) this just "teleports" the camera in the Y position.

What am I doing wrong? Can someone help me solve this?

And sorry about my english...

         private void FixedUpdate()
         {
             float speed;
             GetInput(out speed);
             // always move along the camera forward as it is the direction that it being aimed at
             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
 
             // get a normal for the surface that is being touched to move along it
             RaycastHit hitInfo;
             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
                                m_CharacterController.height/2f);
             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
 
             m_MoveDir.x = desiredMove.x*speed;
             m_MoveDir.z = desiredMove.z*speed;
 
             if (m_CharacterController.isGrounded)
             {
                 m_MoveDir.y = -m_StickToGroundForce;
 
                 if (m_Jump)
                 {
                     m_MoveDir.y = m_JumpSpeed;
                     PlayJumpSound();
                     m_Jump = false;
                     m_Jumping = true;
                 }
             }
             else
             {
                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
             }
             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
 
             ProgressStepCycle(speed);
             UpdateCameraPosition(speed);
 
             if (m_IsCrouching) 
             {
                 m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
                 
                 if (m_CharacterController.height > m_ccHeight / 2f)
                 {
                     m_CharacterController.height = m_ccHeight / 2f;
                     m_CharacterController.center = new Vector3(0f, m_ccCenter.y /2f, 0f);
                 }
             }
             else
             {
                 Ray crouchRay = new Ray(transform.position + Vector3.up * m_CharacterController.radius * k_Half, Vector3.up);
                 float crouchRayLength = m_ccHeight - m_CharacterController.radius * k_Half;
                 if (Physics.SphereCast(crouchRay, m_CharacterController.radius * k_Half, crouchRayLength))
                 {
                     m_IsCrouching = true;
                     m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
                     return;
                 }
                 Debug.DrawRay(transform.localPosition, Vector3.up, Color.red,crouchRayLength);
             }
 
             if (!m_IsCrouching)
             {

                     m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_OriginalCameraPosition.y, ref velocity, m_StandSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
                 
                 if (m_CharacterController.height < m_ccHeight)
                 {
                     m_CharacterController.height = m_ccHeight;
                     m_CharacterController.center = new Vector3(0f, m_ccCenter.y, 0f);
                 }
                 Ray crouchRay = new Ray(transform.position + Vector3.up * m_CharacterController.radius * k_Half, Vector3.up);
                 float crouchRayLength = m_ccHeight - m_CharacterController.radius * k_Half;
                 if (Physics.SphereCast(crouchRay, m_CharacterController.radius * k_Half, crouchRayLength))
                 {
                     m_IsCrouching = true;
                     m_Camera.transform.localPosition = new Vector3(m_Camera.transform.localPosition.x, Mathf.SmoothDamp(m_Camera.transform.localPosition.y, m_Camera.transform.localPosition.y / 2f, ref velocity, m_CrouchSpeed * Time.fixedTime), m_Camera.transform.localPosition.z);
                 }
                 Debug.DrawRay(transform.localPosition, Vector3.up, Color.blue, crouchRayLength);
             }
             Debug.LogWarning(m_Camera.transform.localPosition.y);
         }

 

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

27 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

Related Questions

please help me, i'm creating a first person game, but my script is incomplete and i can't crouch 0 Answers

Mathf.Clamp does not work correctly in trannsform.position I'm trying to create a 2.5D game but I'm having trouble limiting camera movement 0 Answers

[C#] playerHeight not working for FPS Controller 2 Answers

Correctly smooth crouch script? 2 Answers

Spaceship steering limit 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