Search Unity

Problem Jumping runs infinity

Discussion in 'Scripting' started by wafer123, Aug 11, 2019.

?

good

  1. 1

    0 vote(s)
    0.0%
  2. 2

    0 vote(s)
    0.0%
  1. wafer123

    wafer123

    Joined:
    Aug 10, 2019
    Posts:
    2




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [RequireComponent(typeof(Animator))]
    5. [RequireComponent(typeof(Rigidbody))]
    6. public class Mover : MonoBehaviour
    7. {
    8. public float movementspeed = 50f;
    9. public float speedrotation = 600.0f;
    10. private Animator anim;
    11. public float x, y;
    12. public string InputButton = "Jump";//Now you can access from the editor and simply change the input to your liking
    13. public string AxisX = "Horizontal";
    14. public string AxisY = "Vertical";
    15. Rigidbody Rb;
    16. public float push = 1500f;//Jump power
    17. public float GroundCheck = 0.5f;
    18. public float Compensation = 0.5f;//from where the lightning starts
    19. public bool it is on the floor; //is on the ground or not
    20. void Start()
    21. {
    22. //Get the Components
    23. anim = GetComponent<Animator>();
    24. Rb = GetComponent<Rigidbody>();
    25. }
    26. void Update()
    27. {
    28. //Ground Check
    29. Ground Check();
    30. //call the function MoveCharacter
    31. MovePlayer();
    32. //call the function Jumping
    33. if (Input.GetButtonDown(InputButton) && it is on the floor)
    34. {
    35. Jump.player();
    36. }
    37. }
    38. private void Ground Check() //Ground Check
    39. {
    40. Ray ray;
    41. ray = new Ray(transform.position - new Vector3(0, Compensation, 0), -transform.up* Ground Check);
    42. if (Physics.Raycast(ray, Ground Check))
    43. {
    44. NoJump();
    45. }
    46. else
    47. {
    48. IsJump();
    49. }
    50. }
    51. private void jumpplayer()//jplayer jump
    52. {
    53. Rb.AddForce(transform.up * push);/add a force on the y axis, and if you move, jump forward
    54. anim.SetBool("Jump",true);
    55. }
    56. private void MovePlayer(float x =0,float y=0)//mpve player
    57. {
    58. x = Input.GetAxis("Horizontal");
    59. y = Input.GetAxis("Vertical");
    60. anim.SetFloat("VelX", x);
    61. anim.SetFloat("VelY", y);
    62. //This moves the player on the axes
    63. Vector3 Move = new Vector3(x, 0.0f,y);
    64. Rb.AddForce(Move* movementspeed);
    65. // This rotates the player when going to another address
    66. if (Move != Vector3.zero)
    67. {
    68. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Move.normalized), 0.2f);
    69. }
    70. }
    71. void NoJump()
    72. {
    73. it is on the floor= true;
    74. anim.SetBool("Jump", false);
    75. }
    76. void IsJump()
    77. {
    78. it is on the floor = false;
    79. }
    80. private void OnDrawGizmos()//Draw the lightning in the editor
    81. {
    82. Debug.DrawRay(transform.position - new Vector3(0, Compensation, 0), -transform.up * groundCheck, Color.magenta);
    83. }
    84. }





    Very good morning I am trying to solve the problem of my character since it jumps infinitely attached the images corresponding to my project I will also attach my project the character automatically jumps the idea is that with the space bar when pressing jump without having to start it automatically

    I attach my project :https://drive.google.com/drive/folders/1M1pk3Mo1yhrfl2xYJmb9bK6oNkmGUXRG?usp=sharing
     
    Last edited: Aug 11, 2019
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Do not link to outside sources please, almost no one is going to bother navigating away from the Unity forums without a good reason.

    If you have screenshots, use the Upload File feature to include them. If you have videos, make sure they're properly embedded.

    Don't just include your whole project, no one's going to download and open it unless they're just in a crazy bored/altruistic mood. We probably don't have exactly the same Unity version as you anyways, so the results would be suspect in many cases. Rather, post the relevant code in code tags instead.

    You can't edit and add code tags after you've already posted the code without them (as you've done here) since the formatting isn't saved- it's impossible to read this way. Please re-copy the code from the source, then paste directly into code tags again, or using the Insert Code button / pop-up window in the editor.

    Please do not make polls unnecessarily, it's distracting and usually serves no purpose. If there's any question at all in whether it's necessary, then don't make one.

    Do not double post if you feel that you aren't getting a response fast enough.

    Please describe the problem, the expected results, and the things you've already tried. Please use punctuation when you do so, so that we can actually understand what you're saying.
     
    Last edited: Aug 11, 2019