Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 stanferrolino12 · Oct 14, 2020 at 05:26 AM · rigidbodycharactercontrollerdouble jump

Any tips to fix and improve this movement script?

I have a movement script that I created which is supposed to have double jumping and diving. I have experience in scripting but I am new to unity (The original script was ripped off from a Brackeys tutorial), so my script was very sloppy. This gave way to many bugs in the script (infinite double jumps, movement doesn't stop after moving, etc), which I can't fix, because I really dont understand much about the movement system. Any help on how to fix it?

 public class Movement : MonoBehaviour
 {
     Rigidbody Rb;
     Collider Collider;
     public GameObject Cameraboi;
     public float Speed;
     public float jumpheight;
     public float FaceDamping;
     public bool DoubleJump;
     public float DoubleJumpHeight;
     public bool Dive;
     bool Grounded;
     public LayerMask Jumpable;
     float AngleVelocity;
     [Range(0, 10)]
     public float GroundCheckHeight;
     [Range(0, 10)]
     public float GroundCheckRadius;
 
     bool Jumped;
     bool CanDoubleJump;
     bool DoubleJumped;
     bool CanDive;
     bool Comboed;
 
     public GameObject LandJump;
     public GameObject AirJump;
     public GameObject AirDive;
     public GameObject RunParticles;
     
     
     // Start is called before the first frame update
     void Start()
     {
         Rb = GetComponent<Rigidbody>();
         Collider = GetComponent<Collider>();
         Cameraboi = Camera.main.gameObject;
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     // Update is called once per frame
     void Update()
     {
         Grounded = CheckGround();
 
         float Xaxis = Input.GetAxisRaw("Horizontal");
         float Zaxis = Input.GetAxisRaw("Vertical");
 
         Vector3 GoalVelocity = new Vector3(Xaxis, 0, Zaxis);
 
         
         if(GoalVelocity.magnitude > 0.5)
         {
             
             float TargetAngle = Mathf.Atan2(GoalVelocity.x, GoalVelocity.z) * Mathf.Rad2Deg + Cameraboi.transform.eulerAngles.y;
             float SmoothAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, TargetAngle, ref AngleVelocity, FaceDamping);
             transform.rotation = Quaternion.Euler(0f, SmoothAngle, 0);
             Vector3 RealVelocity = Quaternion.Euler(0f, TargetAngle, 0) * Vector3.forward;
             Rb.velocity = new Vector3(RealVelocity.x * Speed, Rb.velocity.y, RealVelocity.z * Speed);
         }
         else if(Grounded)
         {
             Rb.velocity = new Vector3(0, Rb.velocity.y, 0);
         }
 
         
 
         if (Grounded && GoalVelocity.magnitude > 0.5)
         {
             
             if(!RunParticles.GetComponent<ParticleSystem>().isPlaying)
             {
                 RunParticles.GetComponent<ParticleSystem>().Play();
             }
         }
         else
         {
             RunParticles.GetComponent<ParticleSystem>().Stop();
         }
 
         
 
         
         
 
         if(Grounded && Input.GetButtonDown("Jump") == true)
         {
             Rb.AddForce(new Vector3(0, jumpheight),ForceMode.Impulse);
             LandJump.GetComponent<ParticleSystem>().Play();
             Jumped = true;
             
         }
 
         if (DoubleJump && Input.GetButtonDown("Jump") == true)
         {
             if (Comboed == true)
             {
                 CanDoubleJump = false;
 
             }
 
             if (CanDoubleJump == true)
             {
                 
                 Rb.AddForce(new Vector3(0, DoubleJumpHeight), ForceMode.Impulse);
                 AirJump.GetComponent<AudioSource>().Play();
                 CanDoubleJump = false;
                 DoubleJumped = true;
                 Jumped = false;
                 
             }
         }
 
         if(Dive && Input.GetButtonDown("Jump") == true)
         {
             if(Comboed == true)
             {
                 CanDive = false;
 
             }
 
             if(CanDive == true)
             {
                 if (GoalVelocity.magnitude < 0.5)
                 {
                     Rb.AddRelativeForce(new Vector3(0, 1f, 20f), ForceMode.Impulse);
                     AirDive.GetComponent<AudioSource>().Play();
                     AirDive.GetComponent<ParticleSystem>().Play();
                     Comboed = true;
                     CanDive = false;
                     DoubleJumped = false;
                     Jumped = false;
                     Comboed = true;
                 }
 
             }
         }
 
         if (Grounded == false && Comboed == false)
         {
             Invoke("SetCanDoubleJump", 0.3f);
             Debug.Log("setcandouble");
         }
 
         if(DoubleJumped == true && Comboed == false)
         {
             Invoke("SetCanDive", 0.2f);
             Debug.Log("setcandove");
         }
 
         if(Grounded && !Jumped)
         {
             Jumped = false;
             CanDoubleJump = false;
             CanDive = false;
             DoubleJumped = false;
             Comboed = false;
         }
         
     }
 
     bool CheckGround()
     {
         Collider[] Hits = Physics.OverlapSphere(new Vector3(transform.position.x, transform.position.y - GroundCheckHeight, transform.position.z), GroundCheckRadius, Jumpable);
         bool Outcome = false;
         
         if(Hits.Length > 0)
         {
             Outcome = true;
         }
         return Outcome;
     }
 
     void SetCanDoubleJump()
     {
 
         CanDoubleJump = true;
        
         
     }
 
     void SetCanDive()
     {
         CanDive = true;
 
 
     }
 }
 
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

206 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

Related Questions

character controller + physics 1 Answer

Sphere + rigidbody + character controller 1 Answer

Difference between a rigidbody and a character controller? 1 Answer

3rd person Character Control with kinect 0 Answers

What is the minimum you need for collision detection to work? 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