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 V3ndetta999 · Mar 29, 2019 at 12:17 PM · scripting problemfpscontrollercrouching

Unity 5 FPS crouch camera is pushed upwards by the controller[SOLVED!]

Hello everyone. For some reason, my script that smoothly resizes the fps controller height and lerps the camera position does not work as intended. Every time my player stands back up, the camera is gradually pushed upwards giving a very glitchy motion. I looked through scripts and forums, tried a multitude of combinations on my own, but I am missing something. Why is the camera pushed upwards bit by bit? If anyone knows that the problem might be, I'd appreciate an answer. I am clearly missing something very easy but I still can't find it and it's extremely frustrating as crouching is an integral part of my game.Here is my script:

 public class Crouch : MonoBehaviour
 {
     public CharacterController MyChar;
     public Camera MyCam;
 
     public float CrouchWalkSpeed;
     public float CrouchHeight = 1.0f;
 
     public float SmoothTime;
 
     private float DefCamY;
 
     private void Start()
     {
         DefCamY = MyCam.transform.position.y;
     }
 
     private void Update()
     {
         if (Input.GetKey(KeyCode.LeftControl))
         {
             MyChar.height = Mathf.Lerp(MyChar.height, CrouchHeight, Time.deltaTime * SmoothTime);
             var posY = MyCam.transform.position.y;
             posY = Mathf.Lerp(DefCamY, CrouchHeight, Time.deltaTime * SmoothTime);
         }
         else
         {
             MyChar.height = Mathf.Lerp(MyChar.height, 1.8f, Time.deltaTime * SmoothTime);
             var posY = MyCam.transform.position.y;
             posY = Mathf.Lerp(MyCam.transform.position.y, DefCamY, Time.deltaTime * SmoothTime);
 
         }
     }
 
 
 }




EDIT: After a few hours of frustration, looking through the way Unity's fps controller works, I have come up with a new script. This one Does not move the camera, as it conflicts with the camera position implemented in the FPS controller. The fps controller script always tries to keep the camera at head level, which is about height(in our case 1.8) divided by 2 ===> 1.8/2=0.9 . What my new script does is as follows:

  1. Lerp the char controller height

  2. Lerp the char controller center

  3. If the player is not crouching, set the Y position of the game object to height/2.

results and problems: A smooth crouch and stand up, however the head bob(if used) can be glitchy(will need further implementation or skipping it's usage which I will do anyway:) Jumping is not working unless we implement the position to not be forced to the ground(which is quite easy tbh:) )

Here is my new script, anyone feel free to use it if you might need it. I will follow up with an improved crouch system soon and post it for everyone once I have worked it out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Crouch : MonoBehaviour
 {
     public GameObject PlayerObject;
 
     public CharacterController MyChar;
     public Camera MyCam;
 
     public float CrouchWalkSpeed;
     public float CrouchHeight = 1.0f;
 
     public float SmoothTime;
 
     private float DefCamY;
 
     private float centY;
     public bool isCrouching = false;
 
     private void Start()
     {
         DefCamY = MyCam.transform.position.y;
     }
 
     private void Update()
     {
          var centY= MyChar.center.y; //Always update centY so it corresponds to the current Y coordonate of the character controller height
 
 
         if (Input.GetKey(KeyCode.LeftControl))
         {
             MyChar.height = Mathf.Lerp(MyChar.height, CrouchHeight, Time.deltaTime/ SmoothTime);
 
             centY = Mathf.Lerp(centY, CrouchHeight, Time.deltaTime/SmoothTime);
             MyChar.center = new Vector3(0, centY, 0);
 
             if (MyChar.height != 1.8f)
             {
                 isCrouching = true;
             }
         }
         else
         {
             isCrouching = false;
 
         }
         if (!isCrouching)
         {
             MyChar.height = Mathf.Lerp(MyChar.height, 1.8f, Time.deltaTime/SmoothTime);
             centY = Mathf.Lerp(centY, 0f, Time.deltaTime * SmoothTime);
             MyChar.center = new Vector3(0, centY, 0);
             
                 PlayerObject.transform.position = new Vector3(PlayerObject.transform.position.x, MyChar.height/2, PlayerObject.transform.position.z);
  
         }
     }
 
 
 }
 



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

1 Reply

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

Answer by Vollmondum · Mar 29, 2019 at 01:04 PM

You're missing shifting var posY outside both ifs.

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 V3ndetta999 · Mar 29, 2019 at 04:29 PM 0
Share

Thank you for your answer, while you were right, I also found a better solution to use which gives less of a headache:)

avatar image Vollmondum V3ndetta999 · Mar 29, 2019 at 06:14 PM 0
Share

Yeah, better solution in most cases exclude using characterController :)

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

242 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

create action on touch release 1 Answer

UnassignedReferenceException: The variable TheKnife of MeleeSystem has not been assigned. 2 Answers

[SOLVED]The script don't execute after scene reloading 1 Answer

It does not work removing items from the inventory 0 Answers

UI Appear not appearing after build 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