Search Unity

problem in movement (jump)

Discussion in 'Scripting' started by niffe33_unity, May 17, 2019.

  1. niffe33_unity

    niffe33_unity

    Joined:
    May 13, 2019
    Posts:
    3
    Hi , how i can edit this code , because when i click W and Space , and when controller touches ground , Controler have move lock problem.

    https://pastebin.com/AiYjrrM2

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class chodzenie : MonoBehaviour {
    6.     float speed = 4f;
    7.     float rotspeed = 80f;
    8.     float rot = 0f;
    9.     float gravity = 4f;
    10.     Vector3 moveDir = Vector3.zero;
    11.     public float jumpSpeed = 4f;
    12.     CharacterController controller;
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.         controller = GetComponent<CharacterController>();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (controller.isGrounded) {
    23.             if (Input.GetKey(KeyCode.W)) {
    24.              
    25.                     moveDir = new Vector3(0, 0, 1);
    26.                     moveDir *= speed;
    27.                     moveDir = transform.TransformDirection(moveDir);
    28.                
    29.  
    30.             }
    31.             if (Input.GetKeyUp(KeyCode.W))
    32.             {
    33.                 moveDir = new Vector3(0, 0, 0);
    34.              
    35.             }
    36.  
    37.             if (Input.GetKeyUp(KeyCode.Space)) {
    38.  
    39.                 moveDir.y = jumpSpeed;
    40.             }
    41.  
    42.         }
    43.  
    44.      
    45.  
    46.         rot += Input.GetAxis("Mouse X") * rotspeed * Time.deltaTime;
    47.         transform.eulerAngles = new Vector3(0, rot, 0);
    48.         moveDir.y -= gravity * Time.deltaTime;
    49.         controller.Move(moveDir*Time.deltaTime);
    50.  
    51.     }
    52. }
    53.  
     
    MikawasakiDigital likes this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What is a "move lock problem"? As written, nothing happens when space is pressed, only when space is released.

    If you released space right before you release W, then line 33 will set moveDir.y to 0 when you probably don't want the jump canceled.
     
  3. niffe33_unity

    niffe33_unity

    Joined:
    May 13, 2019
    Posts:
    3
    When I jump or I will fall, but before I press the W key, and release W key in the air, the character will keep moving . Try jump and release W.