Noob Coding Help! :P

I am attempting to put a few videos worth of code together on one script and have run into a problem. I have a cube that I am able to move, jump, and charged jump with. I created an animation for the charged jump and after adding it to the script, am now unable to move, or jump. The animation happens (the cube “crouches”), but when I release the jump, it does not leave the ground. I have seen comments in the video about his “logic” being a bit unsound. If anyone sees this and could take a look at my script, I would greatly appreciate it!

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

public class CubeMove : MonoBehaviour {
   
    private bool onGround;
    private float jumpPressure;
    private float minJump;
    private float maxJumpPressure;
    private Rigidbody rbody;
    private Animator anim;

    public float moveSpeed;

    // Use this for initialization
    void Start ()
    {
        moveSpeed = 10f;

        onGround = true;
        jumpPressure = 0f;
        minJump = 2f;
        maxJumpPressure = 10f;
        rbody = GetComponent<Rigidbody> ();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update ()
    {

        transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
        {
            if (onGround)
            {
                //holding jump button//
                if (Input.GetButton ("Jump"))
                {
                    if (jumpPressure < maxJumpPressure)
                    {
                        jumpPressure += Time.deltaTime * 10f;
                    }
                    else
                    {
                        jumpPressure = maxJumpPressure;
                    }
                    anim.SetFloat ("jumpPressure", jumpPressure + minJump);

            }
            //not holding jump button//
            else
            {
                    //jump//
                    if (jumpPressure > 0f)
                    {
                        jumpPressure = jumpPressure + minJump;
                        rbody.velocity = new Vector3 (jumpPressure / 10, jumpPressure, 0f);
                        jumpPressure = 0f;
                        onGround = false;
                        anim.SetFloat ("jumpPressure",0f);
                        anim.SetBool ("onGround",onGround);
                    }

                }
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.CompareTag ("ground"))
        {
            onGround = true;
            anim.SetBool ("onGround", onGround);
        }
    }
}

[SOLVED] but would still appreciate any input on optimizing the code!

I had to delete the “Position” properties within my animation key frames. The cube was getting stuck inside its own animations. Neat stuff!

Not keen on downloading random files. Common etiquette for others to view your script is to put it in code tags for all to view on the page itself.

and here I was thinking the other way would be the polite option XD I will edit the main post

Even from a usability standpoint, viewing your script from a file requires:

  1. clicking it
  2. selecting a save location
  3. download it, and great now i have a file i don’t really need on my computer
  4. open it in a text editor.

As compared to viewing it from the page in code tags

  1. view it

Should note that if you have a lot of code, it can make sense to upload it to Pastebin with the C# syntax selected and just link it here instead of using code tags. That’s almost never a good idea though, because if you have that much code for people to look through, you likely haven’t done any work to narrow down the scope of the problem you’re having with logs and breakpoints and the like. Code tags are best, by far.

1 Like

The tidy scroll box it has the code in above would have assuaged my initial inclination to link the file…did not know it would do that. I feel like I’ve seen a few mile-long posts before and definitely felt that people would rather just download the script. Thank you both for your helpful input! The point Lysander makes about doing the work to narrow down the scope of a problem is motivation to practice and understand all of this to my utmost ability, as to not put unnecessary work onto others. I will do my best to refrain from such practices going forward. Y’all rock!