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 /
avatar image
0
Question by Imamul125 · Oct 10, 2018 at 08:38 AM · animationerrorthird person controllercapsulecollider

what is wrong with this script

i have a third person character which uses rigid body. i want to change the height of the capsule collider as soon as my character jumps and return back the height capsule collider as soon as it is grounded. i am using curve in my animation to achieve this. but i don't understand what is wrong. my capsule collider height become zero as soon as i press jump. please note the collider height increases but become zero at end.
alt text

    {
    public float InputX;
     public float InputZ;
     public Vector3 desiredMoveDirection;
     public bool blockRotationPlayer;
     public float desiredRotationSpeed;


     public Animator anim;
     public float Speed;
     public float allowPlayerRotation;
     public Camera cam;
     public CharacterController controller;
     public bool isGrounded = false;
     private float verticalVel;
     private Vector3 moveVector;
     public float walkSpeed;
     public float runspeed;
     public bool Lshift;
     public bool Lcntrl;
     private bool jump = true;
     private Rigidbody rb;
     public float jumpDistance;
     public float jumpHeight;
     public float distToGround;
     public bool jumpUP = false ;
     public float time_to_down;
     
     
 
 
    
 
 
 
     // Use this for initialization
     void Start()
     {
         anim = this.GetComponent<Animator>();
         cam = Camera.main;
         rb = GetComponent<Rigidbody>();
         distToGround = GetComponent<Collider>().bounds.extents.y;
        
         
 
 
         
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         
         InputMagnitude();
                       
     }
 
     void PlayerMoveAndRotation()
     {
         InputX = Input.GetAxis("Horizontal");
         InputZ = Input.GetAxis("Vertical");
 
 
         var camera = Camera.main;
         var forward = cam.transform.forward;
         var right = cam.transform.right;
 
         forward.y = 0f;
         right.y = 0f;
 
         forward.Normalize();
         right.Normalize();
 
         desiredMoveDirection = forward * InputZ + right * InputX;
 
         if (blockRotationPlayer == false)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), desiredRotationSpeed);
 
         }
 
     }
 
     void InputMagnitude()
     {
         Lshift = Input.GetKey(KeyCode.LeftShift);
         
         
         //calculate input
         InputX = Input.GetAxis("Horizontal");
         InputZ = Input.GetAxis("Vertical");
 
 
         anim.SetFloat("InputZ", InputZ, 0.0f, Time.deltaTime * 2f);  //from animator float of InputX is called
         anim.SetFloat("InputX", InputX, 0.0f, Time.deltaTime * 2f); //from animator float of InputX is called
 
         //calculate the input magnitude
         Speed = new Vector2(InputX, InputZ).sqrMagnitude;
         // Grounded();
        
         IsGrounded();
         isJump(Speed);
         roll(Speed);
         
 
         if (Speed > allowPlayerRotation)
         {
             
             if ((Lshift) && (isGrounded))
             {
                 
                 anim.SetFloat("InputMagnitude", Speed * 1.0f, 0.5f, Time.deltaTime);
                 //transform.Translate(Vector3.forward * Speed * runspeed * Time.deltaTime);
                 rb.AddForce(transform.forward *Speed * runspeed, ForceMode.VelocityChange);
                 PlayerMoveAndRotation();
 
 
 
             }
             else if ((Lshift == false) && (isGrounded))
             {
              anim.SetFloat("InputMagnitude", Speed * 0.5f, 0.0f, Time.deltaTime);
      rb.AddForce(transform.forward * Speed * walkSpeed, ForceMode.VelocityChange);
                 PlayerMoveAndRotation();
                 
 
             }
 
 
 
 
         }
 
         else if (Speed < allowPlayerRotation)
         {
            
             anim.SetFloat("InputMagnitude", Speed, 0.0f, Time.deltaTime);
             
         }
     }
 
     void isJump(float Speed)
     {
         jump = Input.GetKey(KeyCode.Space);
         if (jump && isGrounded && Speed==0)
         {
             
             anim.SetBool("jumpUP", true);
             rb.AddForce(transform.up * jumpHeight, ForceMode.VelocityChange);
 
         }
             else
             {
                 anim.SetBool("jumpUP", false);
             }
         
         if (jump && Speed > 0 && isGrounded)
             {
             anim.SetBool("IsJump", true); 
             anim.SetFloat("jumDownSpeed", 0.3f);
             GetComponent<CapsuleCollider>().height = anim.GetFloat("jump_up_coll");
            
 
             rb.AddForce(transform.forward * jumpDistance, ForceMode.VelocityChange);
 
 
             Invoke ("jumpDown", time_to_down);
             }
             else
             {
                 anim.SetBool("IsJump", false);
         }
          }
 
 
        void IsGrounded()
     {
         if (!Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f))
         {
             isGrounded = true;
             
         }
         else
         {
             isGrounded = false;
             
         }
     }
   
 
     void jumpDown()
     {
         anim.SetFloat("jumDownSpeed", 1.0f);
       //  GetComponent<CapsuleCollider>().height = anim.GetFloat("jump_down_coll");
     }
     void roll(float Speed)
     {
         Lcntrl = Input.GetKey(KeyCode.LeftControl);
         if (Speed>0 && isGrounded && Lcntrl)
         {
             anim.SetBool("StandRoll", true);
         }
         else
         {
             anim.SetBool("StandRoll", false);
         }
     }
     
 } 


screenshot-898.png (61.6 kB)
screenshot-896.png (163.4 kB)
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

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

Timeline error 1 Answer

Animation Event issue what am I doing wrong here 2 Answers

C# Enum for Animation Issue 1 Answer

Mecanim BlendTree non-linear behaviour? 1 Answer

Can I make animations snap to a frame? 1 Answer


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