• 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
Question by WeirderChimp · Jun 14, 2014 at 06:38 AM · colliderlerp

lerping colliders heights problem

hey i have a character controller and im changing the offset and height of my charactercontrollers capsule. my code:

 void SetCol(string name)// this sets the colliders for the different states
     {
         if (name == "stand")
         {
             currentColRadius = colStandRadius;
             currentColHeight = colStandHeight;
             currentColOffset = colStandOffset;
         }else if(name == "crouch")
         {
             currentColRadius = colCrouchRadius;
             currentColHeight = colCrouchHeight;
             currentColOffset = colCrouchOffset;
         }else if(name == "prone")
         {
             currentColRadius = colProneRadius;
             currentColHeight = colProneHeight;
             currentColOffset = colProneOffset;
         }
     }


and in my update function im using

 charController.center = Vector3.Lerp(charController.center, currentColOffset, colChangeLerpRate * Time.deltaTime);
         charController.height = Mathf.Lerp(charController.height, currentColHeight, colChangeLerpRate * Time.deltaTime);
         charController.radius = Mathf.Lerp(charController.radius, currentColRadius, colChangeLerpRate * Time.deltaTime);


the problem im having is that even though every thing is being lerped by colchangelerprate * time.deltatime my offset is taking longer to reach its destination/target value. and i don't know why. so when my character controllers height is finished changing its on the same level and then the offset makes it move into the ground and fall through it

How do i fix THIS?????

Thanks ~Scott

Comment

People who like this

0 Show 0
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

Answer by StewVanB · Jun 14, 2014 at 08:42 AM

You will have better luck with these collider changes if you use Mathf.MoveTowards. I wrote you a C# script to show you how I would approach this issue. I hope this starts you down the right path!

StandCrouch.cs

Instructions: Place this script on a GameObject that has a BoxCollider and disable or remove the "Mesh Renderer". If you press the up arrow you will increase your stance until you are standing. If you press the down arrow you will decrease your stance until prone.

 using UnityEngine;
 using System.Collections;
 
 public class StandCrouch : MonoBehaviour {
     
     private Stance stance = Stance.Standing;
     
     private Vector3 standingColliderSizeValues = new Vector3(1.0f,2.0f,1.0f);
     private float standingCenterHeight = 1.0f;
     
     private Vector3 crouchingColliderSizeValues = new Vector3(1.0f,1.0f,1.0f);
     private float crouchingCenterHeight = 0.5f;
     
     private Vector3 proneColliderSizeValues = new Vector3(1.0f,0.5f,1.0f);
     private float proneCenterHeight = 0.25f;
     
     
     public float speed = 1.0f;
     
     IEnumerator ChangeStance(Stance _stance,float _speed){
         bool done = false;
         Vector3 targetColliderSizeValues = new Vector3();
         float targetCenterHeight = 0.0f;
         
         switch(_stance){
             case Stance.Standing:
                 targetColliderSizeValues = standingColliderSizeValues;
                 targetCenterHeight = standingCenterHeight;
                 break;
             case Stance.Crouching:
                 targetColliderSizeValues = crouchingColliderSizeValues;
                 targetCenterHeight = crouchingCenterHeight;
                 break;
             case Stance.Prone:
                 targetColliderSizeValues = proneColliderSizeValues;
                 targetCenterHeight = proneCenterHeight;
                 break;
         }
         
         BoxCollider boxCollider = gameObject.GetComponent<BoxCollider>();
         Vector3 tempSize = boxCollider.size;
         float tempHeight = boxCollider.center.y;
         
         while(!done){
             tempSize = Vector3.MoveTowards(tempSize,targetColliderSizeValues,_speed * Time.deltaTime);
             tempHeight = Mathf.MoveTowards(tempHeight,targetCenterHeight,_speed/2 * Time.deltaTime);
             boxCollider.size = new Vector3(tempSize.x,tempSize.y,tempSize.z);
             boxCollider.center = new Vector3(boxCollider.center.x,tempHeight,boxCollider.center.z);
             if(boxCollider.center.y == targetCenterHeight && boxCollider.size == targetColliderSizeValues){
                 done = true;
             }
             yield return null;
         }
     }
     
     void HandleInput(){
         if(Input.GetKeyDown(KeyCode.UpArrow)){
             int currentStance = (int)stance;
             currentStance += 1;
             if(currentStance > (int)Stance.Standing){
                 currentStance = (int)Stance.Standing;
             }
             stance = (Stance)currentStance;    
         }
         else if(Input.GetKeyDown(KeyCode.DownArrow)){
             int currentStance = (int)stance;
             currentStance -= 1;
             if(currentStance < (int)Stance.Prone){
                 currentStance = (int)Stance.Prone;
             }
             stance = (Stance)currentStance;
         }
         StartCoroutine(ChangeStance(stance,speed));
     }
     
     void Update(){
         HandleInput();    
     }
 }
 
 public enum Stance{
     Prone,
     Crouching,
     Standing
 }
 
Comment
WeirderChimp

People who like this

1 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 WeirderChimp · Jun 14, 2014 at 09:25 AM 0
Share

would this work with a character controller because im using a character controllers collider. if so how would i do that???

thanks for replaying ~Scott

avatar image StewVanB · Jun 15, 2014 at 03:44 AM 0
Share

Yes this should still work, you will need to make sure that you are using values for your type of collider. For example, for the character controller you will use CharacterController instead of BoxCollider. A character controller also uses a Capsule so instead of a Vector3 for size you will have a height and a radius.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Adding 1 point after collison 1 Answer

how to limit the counting of multiple trigger collisions 1 Answer

Lerp position with curved paths 2 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