Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player colliding with water layer Due to gravity!

Discussion in 'Physics' started by alexrayyan, Aug 4, 2022.

?

Colliding with water

  1. To review code or help me to modify the code

    100.0%
  2. Why its happening

    100.0%
Multiple votes are allowed.
  1. alexrayyan

    alexrayyan

    Joined:
    Apr 30, 2020
    Posts:
    1
    This is the Concept of game - I'm creating a game where the player has to jump and cross the lake. If player touches the water then he should be teleported back to the start position.

    I'm facing issue in the jump code where it uses gravity to jump. When i play the game the _directionY is continuously updating gravity downwards with respect to Time.deltatime. Even if the player don't touch water
    the player is triggering water layer again and again.

    Please help me out soon..



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class Player_Move : MonoBehaviour
    {
    Rigidbody rb;
    Score score;
    private Vector3 direction;
    [SerializeField] Transform StartPos;
    [SerializeField] GameObject GameOverScene;
    public float _moveSpeed = 5f;
    //public Vector3 _jumpForward; //moving player forward while jumping
    public float _gravity = 9.81f;
    public float _jumpSpeed = 3.5f;
    public float _doubleJumpMultiplier = 0.5f;
    public CharacterController _controller;
    Animator anim;
    [SerializeField]
    private float _directionY;
    public bool _canDoubleJump = false;
    private bool isJumping;
    private bool jumpPressed = false;
    Timer timer;


    void Start()
    {
    Time.timeScale = 1;
    GameOverScene.SetActive(false);
    rb = GetComponent<Rigidbody>();
    _controller = GetComponent<CharacterController>();
    anim = GetComponent<Animator>();
    score = FindObjectOfType<Score>();
    }

    void Update()
    {
    playermovement();
    //time = time;
    //time = (int)Time.time;
    //TimerText.text = time.ToString();
    //time++;
    }
    public void playermovement()
    {
    float horizontalInput = Input.GetAxis("Horizontal");
    float verticalInput = Input.GetAxis("Vertical");
    //characterController.Move((transform.right * horizontalInput + transform.forward * verticalInput) * Time.deltaTime);
    direction = new Vector3(horizontalInput, 0, verticalInput);
    //Jump();
    //forward
    if (verticalInput > 0)
    {
    if (!isJumping)
    {
    anim.SetBool("isRunning", true);
    }
    }
    else if (isJumping)
    {
    anim.SetBool("isRunning", false);
    }
    //backward
    if (verticalInput < 0)
    {
    anim.SetBool("isRunBack", true);
    }
    else
    {
    anim.SetBool("isRunBack", false);
    }
    if (horizontalInput > 0)
    {
    anim.SetBool("isRightPressed", true);
    }
    else
    {
    anim.SetBool("isRightPressed", false);
    }
    if (horizontalInput < 0)
    {
    anim.SetBool("isLeftPressed", true);
    }
    else
    {
    anim.SetBool("isLeftPressed", false);
    }
    Jump();

    //Because of this its triggering water
    _directionY -= _gravity * Time.deltaTime;
    direction.y = _directionY;
    _controller.Move(direction * _moveSpeed * Time.deltaTime);
    }
    private void Jump()
    {
    jumpPressed = true;
    if (_controller.isGrounded)
    {
    _canDoubleJump = true;
    anim.SetBool("isRunning", false);
    if (Input.GetButtonDown("Jump"))
    {
    isJumping = true;
    anim.SetBool("isRunning", false);
    anim.SetBool("isJump", true);
    _directionY = _jumpSpeed;

    }
    }
    else
    {
    anim.SetBool("isJump", false);
    if (Input.GetButtonDown("Jump") && _canDoubleJump)
    {
    isJumping = true;
    //anim.SetBool("isRunning", false);
    anim.SetBool("isJump", true);
    _directionY = _jumpSpeed * _doubleJumpMultiplier;
    _canDoubleJump = false;
    }
    isJumping = false;
    anim.SetBool("isJump", false);
    }
    jumpPressed = false;
    }
    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Water"))
    {
    Debug.Log("Water");
    //Destroy(gameObject);
    }

    if (other.gameObject.CompareTag("Finish"))
    {
    GameOverScene.SetActive(true);
    //timer.triggeredTimer
    Time.timeScale = 0;
    score.enabled = true;
    //score.score = (int)timer.TimeLeft;
    Debug.Log("Finish Line Touched");

    //Destroy(gameObjec
    //);
    }
    }
    IEnumerator DeathDelay()
    {
    yield return new WaitForSeconds(0.5f);
    transform.position = StartPos.position;
    }
    }
     

    Attached Files:

    • 1.png
      1.png
      File size:
      43.3 KB
      Views:
      122
    • 2.png
      2.png
      File size:
      7.4 KB
      Views:
      125
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    There is a button in the post editor that says "CODE <>". It makes posted code a lot easier to read. This still looks like a formatting mess but at least it's colored:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Player_Move : MonoBehaviour
    6. {
    7. Rigidbody rb;
    8. Score score;
    9. private Vector3 direction;
    10. [SerializeField] Transform StartPos;
    11. [SerializeField] GameObject GameOverScene;
    12. public float _moveSpeed = 5f;
    13. //public Vector3 _jumpForward; //moving player forward while jumping
    14. public float _gravity = 9.81f;
    15. public float _jumpSpeed = 3.5f;
    16. public float _doubleJumpMultiplier = 0.5f;
    17. public CharacterController _controller;
    18. Animator anim;
    19. [SerializeField]
    20. private float _directionY;
    21. public bool _canDoubleJump = false;
    22. private bool isJumping;
    23. private bool jumpPressed = false;
    24. Timer timer;
    25.  
    26.  
    27. void Start()
    28. {
    29. Time.timeScale = 1;
    30. GameOverScene.SetActive(false);
    31. rb = GetComponent<Rigidbody>();
    32. _controller = GetComponent<CharacterController>();
    33. anim = GetComponent<Animator>();
    34. score = FindObjectOfType<Score>();
    35. }
    36.  
    37. void Update()
    38. {
    39. playermovement();
    40. //time = time;
    41. //time = (int)Time.time;
    42. //TimerText.text = time.ToString();
    43. //time++;
    44. }
    45. public void playermovement()
    46. {
    47. float horizontalInput = Input.GetAxis("Horizontal");
    48. float verticalInput = Input.GetAxis("Vertical");
    49. //characterController.Move((transform.right * horizontalInput + transform.forward * verticalInput) * Time.deltaTime);
    50. direction = new Vector3(horizontalInput, 0, verticalInput);
    51. //Jump();
    52. //forward
    53. if (verticalInput > 0)
    54. {
    55. if (!isJumping)
    56. {
    57. anim.SetBool("isRunning", true);
    58. }
    59. }
    60. else if (isJumping)
    61. {
    62. anim.SetBool("isRunning", false);
    63. }
    64. //backward
    65. if (verticalInput < 0)
    66. {
    67. anim.SetBool("isRunBack", true);
    68. }
    69. else
    70. {
    71. anim.SetBool("isRunBack", false);
    72. }
    73. if (horizontalInput > 0)
    74. {
    75. anim.SetBool("isRightPressed", true);
    76. }
    77. else
    78. {
    79. anim.SetBool("isRightPressed", false);
    80. }
    81. if (horizontalInput < 0)
    82. {
    83. anim.SetBool("isLeftPressed", true);
    84. }
    85. else
    86. {
    87. anim.SetBool("isLeftPressed", false);
    88. }
    89. Jump();
    90.  
    91. //Because of this its triggering water
    92. _directionY -= _gravity * Time.deltaTime;
    93. direction.y = _directionY;
    94. _controller.Move(direction * _moveSpeed * Time.deltaTime);
    95. }
    96. private void Jump()
    97. {
    98. jumpPressed = true;
    99. if (_controller.isGrounded)
    100. {
    101. _canDoubleJump = true;
    102. anim.SetBool("isRunning", false);
    103. if (Input.GetButtonDown("Jump"))
    104. {
    105. isJumping = true;
    106. anim.SetBool("isRunning", false);
    107. anim.SetBool("isJump", true);
    108. _directionY = _jumpSpeed;
    109.  
    110. }
    111. }
    112. else
    113. {
    114. anim.SetBool("isJump", false);
    115. if (Input.GetButtonDown("Jump") && _canDoubleJump)
    116. {
    117. isJumping = true;
    118. //anim.SetBool("isRunning", false);
    119. anim.SetBool("isJump", true);
    120. _directionY = _jumpSpeed * _doubleJumpMultiplier;
    121. _canDoubleJump = false;
    122. }
    123. isJumping = false;
    124. anim.SetBool("isJump", false);
    125. }
    126. jumpPressed = false;
    127. }
    128. private void OnTriggerEnter(Collider other)
    129. {
    130. if (other.gameObject.CompareTag("Water"))
    131. {
    132. Debug.Log("Water");
    133. //Destroy(gameObject);
    134. }
    135.  
    136. if (other.gameObject.CompareTag("Finish"))
    137. {
    138. GameOverScene.SetActive(true);
    139. //timer.triggeredTimer
    140. Time.timeScale = 0;
    141. score.enabled = true;
    142. //score.score = (int)timer.TimeLeft;
    143. Debug.Log("Finish Line Touched");
    144.  
    145. //Destroy(gameObjec
    146. //);
    147. }
    148. }
    149. IEnumerator DeathDelay()
    150. {
    151. yield return new WaitForSeconds(0.5f);
    152. transform.position = StartPos.position;
    153. }
    154. }