Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

NEED HELP W/CODE

A topic by hellowz created 43 days ago Views: 498 Replies: 1
Viewing posts 1 to 1

THIS CODE IS EVIL HELP ME FIX PLZZZZZZZZZZZZ

using System.Collections;

using UnityEngine;

public class HeroController : MonoBehaviour

{

    [SerializeField]float speed;

    public float maxspeed;

    [SerializeField] float decel;

    [SerializeField] Rigidbody2D rb;

    [SerializeField] float gravity;

    [SerializeField]bool jumping;

    [SerializeField] bool isacelcont;

    [SerializeField] Transform ray;

    [SerializeField] float consistantgrav;

    [SerializeField] float jumpforce;

    [SerializeField] float raydistance;

    [SerializeField] bool isprecise;

    [SerializeField] float constantantforce;

     [SerializeField]LayerMask groundMask;

      [SerializeField] float groundDistance = 0.2f;

[SerializeField]bool grounded;

[SerializeField] float buffertime;

    [SerializeField] float buffertimer;

    [SerializeField] bool intheair;

    [SerializeField] GameObject oio;

    [SerializeField] float dashforce;

    [SerializeField] GameObject slashBox;

   

    bool canslash;

    [SerializeField] float slashleangle;

    [SerializeField] float cyotetymr;

    //MAKE THE GRAVITY CONTROL TOMORROW, LESS GRAVITY WHEN MOVING MIDAIR, MAKE DASH AND SLASH ATTACK, ALSO MAKE JUMPFORCE HIGHT BUT ADD CODE TO COURUNTINE AND STOP IT UNLESS MOVING

    bool onbuffer;

    // MAKE IT GOOD(juicy)

    void Start()

    {

         consistantgrav=gravity ;constantantforce = jumpforce;

         slashBox.SetActive(false);

         canslash = true;

       

    }

    void Jump()

    {

        if(buffertimer<1&&Input.GetKeyDown(KeyCode.Z))

        {

            onbuffer = true;

            if (grounded&&onbuffer)

            {

                jumpforce = constantantforce;

                jumping = true;

                onbuffer = false;

                grounded = false;

                rb.linearVelocity = new Vector2(rb.linearVelocityX, rb.linearVelocityY * jumpforce);

                Debug.Log("You have clicked the jumpbutton w/ buffer");

            }

        }

        if (!grounded)

        {

            cyotetymr -= Time.deltaTime;

         }

        if (cyotetymr != 0)

        {

            grounded = true;

        }

        else

        {

         }

        if (grounded)

        {

            cyotetymr = 0.2f;

        }

        grounded = Physics2D.OverlapCircle(ray.position,groundDistance, groundMask);

        if (Input.GetKeyDown(KeyCode.Z) && grounded == true)

        {

            jumpforce = constantantforce;

            jumping = true;

            grounded = false;

            rb.linearVelocity = new Vector2(rb.linearVelocityX,  jumpforce);

            Debug.Log("You have clicked the jumpbutton");

        }

        else if (Input.GetKeyUp(KeyCode.Z))

        {

         

            gravity = gravity+0.1f;

            rb.linearVelocity = new Vector2(rb.linearVelocityX, 0);

        }

    }

    void BasicAttack()

    {

        if(Input.GetKeyDown(KeyCode.X)&&canslash)

        {

StartCoroutine(Bas2icAttack());

        }

    }

    void Update()

    {

        BasicAttack();

        rb.linearVelocity = new Vector2(rb.linearVelocityX, rb.linearVelocity.y+-gravity) ;

        MOVEMNT();

       

        if ( grounded)

        {

            buffertimer = buffertime;

            gravity = consistantgrav;

           

            jumping = false;

         }else{

            buffertimer-=Time.deltaTime;

         }

         

        Jump();

    }

    // LOCK IN

    void MOVEMNT()

    {

       

        float x = Input.GetAxis("Horizontal");

        if (isacelcont)

        {

            Vector2 fat = new Vector2(x * maxspeed, rb.linearVelocity.y);

            rb.linearVelocity = Vector2.MoveTowards(

            rb.linearVelocity,

            fat,

            (fat.magnitude > 0 ? speed : decel) * Time.deltaTime);

        }

        else

        {

            rb.linearVelocity = new Vector2(x * speed, rb.linearVelocityY);

        }

              if (x > 0)

            {

                transform.localScale = new Vector3(-transform.localScale.x,transform.localScale.y, 0);

            }

          if (x < 0)

        {

            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, 0);

        }

           

        if (rb.linearVelocity.x < 0)

        {

            transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z - 4);

        }

        else if (rb.linearVelocityX > 0)

        {

            transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z + 4);

        }

        if (rb.linearVelocity.x == 0)

        {

             transform.rotation = Quaternion.Euler(0, 0,0);

         }

         Debug.Log(x);

       

         

   //good progress :) Do more next time, MUCH MORE :):):):(;):O

       

    }

Goblin spotted a few things in the pasted code. It hasn't run your project.

1. Bas2icAttack() is called, but its method isn't included. If this is the whole file, that call needs a matching coroutine method, and the class needs one more closing } at the end. If those were just left out of the post, please share the first Console error.

2. When x > 0, you set the X scale to minus its current value every frame. That keeps switching the sign while you hold right. Set it to a fixed sign based on the direction instead, keeping the original scale magnitude. Which sign faces right depends on your sprite.

3. The buffered jump uses rb.linearVelocityY * jumpforce. If the current Y velocity is zero, the result is still zero; if it's negative, the result stays negative for a positive jumpforce. To launch upward at a chosen speed, assign jumpforce directly, as your normal jump branch already does. That fixes this assignment, but the buffer and coyote-time conditions still need a separate look.

Which Unity version are you using, and what fails first: compiling, jumping, or facing the right way?

Unity's velocity reference: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Rigidbody2D-linearVelocity.html

Deleted 43 days ago