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 potjam100 · Nov 18, 2019 at 11:02 PM · lerpheightcrouching

Can't get lerp working with toggleable crouch

So I found a script in this forum that lets the player crouch and lerps between standing/crouching size. The problem I'm having is that I'm wanting a toggleable crouch state, but modifying the script to include that seems to break the lerp. I can still toggle between crouching/standing and I can see the states changing in the inspector, along with the speed change working fine.

Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController controller;
 
     public float speed = 6f;
     public float sprintSpeed = 10f;
     public float crouchSpeed = 3f;
     public float jumpHeight = 1.5f;
     public float gravity = -20f;
     
     public float startHeight;
     
     public bool isCrouched;
 
     float maxJumpsNum = 1;
     float currentJumpNum = 0;
 
     public Transform groundCheck;
     public float groundDistance = 0.4f;
     public LayerMask groundMask;
 
     Vector3 velocity;
     public bool isGrounded;
 
     void Start()
     {
         controller = GetComponent<CharacterController>();
         startHeight = controller.height;
     }
 
     void Update()
     {
         Movement();
         Crouch();
         Jump();
         WorldHandler();
     }
 
     void Movement()
     {
         //w/s/a/d input.
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
 
         //normalizes the movement vector by making its magnitude, on any of its 
         //axis, extend to the maximum length of 1. stops faster 
         //diagonal movement in this case.
         Vector3 forward = transform.forward;
         Vector3 right = transform.right;
         forward.Normalize();
         right.Normalize();
 
         //a vector3 for the movement input - normalized.
         Vector3 move = (right * x + forward * z).normalized;
 
         
 
         if (Input.GetKey(KeyCode.LeftShift))
         {
             //makes the player sprint.
             controller.Move(move * sprintSpeed * Time.deltaTime);
         }
 
         else
         {
             //makes the player move.
             controller.Move(move * speed * Time.deltaTime);
         }
     }
 
     void Crouch()
     {
         float newHeight = startHeight;
 
         if (Input.GetKeyDown(KeyCode.C))
         {
             if (!isCrouched)
             {
                 newHeight = 0.5f * startHeight;
                 controller.height = Mathf.Lerp(controller.height, newHeight, 5.0f * Time.deltaTime);
                 speed = crouchSpeed;
                 isCrouched = true;
 
             }
 
             else
             {
                 newHeight = 1f * startHeight;
                 speed = 7f;
                 isCrouched = false;
             }
         }
 
         float lastHeight = controller.height;
 
         
 
 
         Vector3 position = transform.position;
         position.y += (controller.height - lastHeight) * 0.5f;
 
     }
 
     void Jump()
     {
         //makes the player jump (mathf square root (h * -v * g)).
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
             currentJumpNum = 0;
         }
         
         //makes the player jump again if the conditions are correct.
         else if (Input.GetKeyDown(KeyCode.Space) && currentJumpNum < maxJumpsNum)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
             currentJumpNum = 1;
         }
 
     }
 
     void WorldHandler()
     {
         //projects a sphere from pre-defined position by [x] distance, looking for a ground layer collision.
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         //applies a tiny amount of downwards velocity constantly to help with ground checking.
         if (isGrounded && velocity.y < 0)
         {
             velocity.y = -2f;
         }
 
         //applies gravity to the player.
         velocity.y += gravity * Time.deltaTime;
         controller.Move(velocity * Time.deltaTime);
     }
 }


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

187 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

Related Questions

Tilting a camera around an object 0 Answers

Acces shader color and lerp 1 Answer

Help with recttransform lerp , lags on mobile not on pc. 1 Answer

Can you make the player do a 180° / u-turn when a button is pressed? 0 Answers

Rigid body vibrating? 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