• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

need help with my health script 1 Answer

Assign variables on Instantiation? 0 Answers

how do i get enemy ai to avoid objects in scene 1 Answer

Setting a bool under a function from another 0 Answers

How do i change this line so that it works as im getting the error Assets/BoxLauncher.cs(28,53): error CS0246: The type or namespace name `ScoreManager' could not be found. Are you missing a using directive or an assembly reference? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges