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 stefizuzu · Apr 23, 2018 at 01:52 AM · c#unity 5collider3djumping

How to make the character walk-jump and walk (and resize the collider when crouched) ?

I am new to unity and I wrote this code and I'm trying to make my character walk even when I press the keys: A S D. I only have an animation that moves forward. I want my character in 3d person, so maybe always moving forward can be fixed with: first turning around then walk (could this make it work or look better ?) (I don't know how to make the camera follow the character but not turing around with him tho). Also, I wish that my character could jump while walking. Now it responds very slow when I press the spacebar key. And last one: I try to resize the Capsule Collider when crouched but this code won't work. Maybe someone know how to fix one of this things? Please help :)

 using UnityEngine;
     using System.Collections;
     
     [RequireComponent(typeof(Animator))]
     [RequireComponent(typeof(Rigidbody))]
     [RequireComponent(typeof(CapsuleCollider))]
 
 public class ActionsC : MonoBehaviour
     {
         private Animator animator;
         CapsuleCollider m_Capsule;
     
         float m_CapsuleHeight;
         Vector3 m_CapsuleCenter;
     
         private void Start()
         {
             animator = GetComponent<Animator>();
          
             // for capsule collider resizing
             m_Capsule = GetComponent<CapsuleCollider>();
             m_CapsuleHeight = m_Capsule.height;
             m_CapsuleCenter = m_Capsule.center;
         }
         private void Update()
         {
             Walk();
             Run();
             Sitting();
             Jump();
             Aiming();
         }
 
         //  idle animation
         public void Stay() 
         {
             animator.SetBool("Aiming", false);
             animator.SetFloat("Speed", 0f);
         }
         public void Walk()
         {
             float xMov = Input.GetAxisRaw("Horizontal");
             float zMov = Input.GetAxisRaw("Vertical");
     
             if (Input.GetKey(KeyCode.W))
             {
                 animator.SetBool("Aiming", false);
                 animator.SetFloat("Speed", 0.5f);
             }
             // I don't have animation for these so idk what to do
             else if (Input.GetKey(KeyCode.W))
             {
                 
             }
             else if (Input.GetKey(KeyCode.A))
             {
     
             }
             else if (Input.GetKey(KeyCode.D))
             {
     
             }
             else Stay();
         }
         public void Jump()
         {
             if (Input.GetKeyDown(KeyCode.Space)){
                 animator.SetBool("Squat", false);
                 animator.SetBool("Aiming", false);
                 animator.SetTrigger("Jump");
                 rigid.AddForce(Vector3.up * 100.0f);  
             }
             // walk and jump
             else if (Input.GetKeyDown(KeyCode.W))
             {
                 if (Input.GetKeyDown(KeyCode.Space))
                 {
                     if (Input.GetKeyDown(KeyCode.LeftShift)) { speed = 40.0f; }
               
                     animator.SetBool("Squat", false);
                     animator.SetFloat("Speed", 0f);
                     animator.SetBool("Aiming", false);
                     animator.SetTrigger("Jump");
                     rigid.AddForce(Vector3.up * 100.0f);
                 }
             }
         }
         //crouching
         public void Sitting()
         {
             if (Input.GetKeyDown(KeyCode.C)){
                 animator.SetBool("Squat", !animator.GetBool("Squat"));
                 animator.SetBool("Aiming", false); 
             }
         }
         // trying to scale the collider when crouched
        void ScaleCapsuleForCrouching(bool crouch)
         {
             if (Input.GetKeyDown(KeyCode.C))
             {
                 if (Input.GetKeyDown(KeyCode.C)) return;
                 m_Capsule.height = m_Capsule.height / 2f;
                 m_Capsule.center = m_Capsule.center / 2f;
             }
         }
     }




Comment
Add comment · Show 1
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 tormentoarmagedoom · Apr 25, 2018 at 11:28 AM 0
Share

Good day.

Your question is correct, but maybe you should do separate questions for each issue.

FAQ :

What are the guidelines for writing good questions?


We want Unity Answers to become a comprehensive database of questions and answers related to Unity. When you ask your questions, it can be helpful to keep a few things in $$anonymous$$d:


Don't write things that will be irrelevant in a few days. Writing that "This is urgent!" will probably not get you faster answers, but it may make people frown at your question.


Some reasons for getting a post rejected:

 There exists duplicate questions with answers if you were to do a search, either on Answers or on the Forum or on Unity's tutorials

 Your question isn't specific enough: asking for a script, asking multiple questions in one post or asking a question that could be either subjective or require extensive discussion

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

232 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

Related Questions

Fast start and ending at jump but slow middle. 0 Answers

How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer

Moving an object in a curve along the z-axis 0 Answers

Still constant jumping even with raycasting. 0 Answers

Collision in the air after jumping 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