Search Unity

Bug UNITY 4.0 C# ISSUE WITH I THINK GRAVITY OR SOMETHINGS LIKE IT

Discussion in 'Editor & General Support' started by nbvcxw57, May 12, 2021.

  1. nbvcxw57

    nbvcxw57

    Joined:
    Mar 30, 2021
    Posts:
    1
    HI,

    First of all, i'm just a beginner, but I tried do create a wall jump system in my 3D game :) ;
    perhaps, it doesnt work, because I think the issue is on line 65 (I added commentary).
    If someone better than me may help me please.

    Thanks.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public class Saut : MonoBehaviour {


    private Vector3 moveVector;
    private Vector3 lastMove;
    private float speed = 8;
    private float jumpforce = 8;
    private float gravity = 25;
    private float verticalVelocity;
    private CharacterController controller;


    [HideInInspector]

    public KeyCode ToucheSaut;


    // Update is called once per frame

    private void Start(){
    controller = GetComponent<CharacterController>();

    }

    private void Update(){
    moveVector = Vector3.zero;

    // Pour se déplacer
    float horizontalInput = Input.GetAxis("Horizontal");
    float verticalInput = Input.GetAxis("Vertical");
    moveVector.x = horizontalInput;
    moveVector.z = verticalInput;

    if(controller.isGrounded){
    // verticalVelocity = -1; It attract us below when player is grounded
    verticalVelocity = 0;

    if(Input.GetKeyDown(ToucheSaut)){
    verticalVelocity = jumpforce;
    }
    }
    else{
    verticalVelocity -= gravity * Time.deltaTime;
    moveVector = lastMove;
    }

    moveVector.y = 0;
    moveVector.Normalize();
    moveVector *= speed;
    moveVector.y = verticalVelocity;

    // Issue in relation with "Pour se déplacer" :
    controller.Move(moveVector * Time.deltaTime);
    lastMove = moveVector;

    }

    private void OnControllerColliderHit(ControllerColliderHit hit){
    if(!controller.isGrounded && hit.normal.y < 0.1f){

    if(Input.GetKeyDown(ToucheSaut)){
    Debug.DrawRay(hit.point,hit.normal,Color.red, 1.25f);
    verticalVelocity = jumpforce;
    moveVector = hit.normal * speed;
    }
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Beyond that, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    You could also just display various important quantities in UI Text elements to watch them change as you playtest.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You said it "doesn't work", but that doesn't really tell us anything. When you want very specific help, you need to describe your issue with the same level of detail. Also, don't post code in a difficult to read fashion. Take a look at how other people are doing it first. When you make it hard for people to help you, you're unlikely to get any help.

    But I just want to point out, if you are new to Unity you should not be starting with Unity version 4.0. That version is about a decade old.