Search Unity

Question I am new to unity and I need your guidance.

Discussion in '2D' started by kpman2300, Oct 6, 2020.

  1. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    I am learning how to make a 2D game through watching Youtube videos. Currently I am trying to learn to script 2D player movement.
    In the given script I am supposed to have limited number of jumps (as much the amount I set) but still I am having infinite jumps.
    Please Help !

    Link to the video I used as a reference for learning :


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

    public class CharacterController2D : MonoBehaviour
    {
    public float speed; //movement speed variable.
    public float jumpForce; //at what force you will jump.
    private float moveInput; //how to move?

    private Rigidbody2D rb;

    private bool facingRight = true; //for flipping character side by side when moving.

    private bool isGrounded; //is character standing on ground? (ground check required!)
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround;

    private int extraJump; //extra jumps!
    public int extraJumpValue;


    void Start()
    {
    extraJump = extraJumpValue;
    rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate() //to manage physics related aspects.
    {
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround); //is grounded or not?

    moveInput = Input.GetAxis("Horizontal"); //built-in unity input field equivalent to holding left or right keys.
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

    if (facingRight == false && moveInput > 0)
    {
    Flip();
    }
    else if(facingRight == true && moveInput < 0)
    {
    Flip();
    }
    }

    void Update ()
    {
    if (isGrounded == true)
    {
    extraJump = extraJumpValue;
    }

    if (Input.GetKeyDown(KeyCode.UpArrow) && extraJump > 0)
    {
    rb.velocity = Vector2.up * jumpForce;
    extraJump--;
    }

    else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJump == 0 && isGrounded == true)
    {
    rb.velocity = Vector2.up * jumpForce;
    }
    }

    void Flip() //flipping character side by side. one of the easy method!
    {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
    }
    }
     
  2. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    244
    Forgive me if im wrong but i think this is more of a question for the Answers section https://answers.unity.com/index.html
    forums are more for discussions when really you just want to know an error in your code.

    You also need to format your code, this is really hard to read and a lot of people wont bother to help at that point.

    but at my guess, you need to set the `extraJumpValue` variable in the inspector. Its probably the default value 0.
     
    kpman2300 likes this.
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Not to contradict Brogan89, but from what I have read in the past the Answers page referenced isnt a great place as most people use these forums. So in my opinion this is in the right spot, but I could also be wrong. I just know you have a way higher chance of getting a response on these forums.

    But, Brogan is probably right, you need to check the inspector. And, use code tags please:
    upload_2020-10-6_15-58-17.png
     
    kpman2300 and eses like this.
  4. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    244
    Thats a good point. Fair enough. I used to go there a lot when I was learning, but now I never do lol
     
    kpman2300 likes this.
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Yeah, Answers is pretty much on life support.
    Most people recommend asking for help here on the forums instead.

    On topic:
    Try reviewing the tutorial again. There's a chance you may have accidentally missed something.
     
    kpman2300 likes this.
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    My guess is that the LayerMask whatIsGround is not set.
     
    kpman2300 likes this.
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @kpman2300

    Please format your code to use codetags.... like already mentioned.

    "I am supposed to have limited number of jumps (as much the amount I set) but still I am having infinite jumps."

    What is the radius of your "isGrounded" check OverlapCircle relative to your character size?

    And have checked its placement so that it is in correct location?
     
    kpman2300 likes this.
  8. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    my actual mistake was that I didn't make a layer dedicated to ground, now I fixed it. thanks for suggestions you provided.
    However I wanna know something new right now (I won't bother you with codes now, sorry about that, @Brogan89 was right.)

    I want to "Fill" inside the square. but I am not able to get it.
    Also, I was wondering, do I need to create a new thread everytime I have questions or doubts? I am afraid if this one becomes dead in the near future.
     

    Attached Files:

    Last edited: Oct 7, 2020
    Brogan89 likes this.
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "do I need to create a new thread everytime I have questions or doubts?"

    Well if you have new question ask it in its own thread if it isn't related to question you asked. Otherwise people who are looking for an answer to the same question will have hard time finding your thread...

    Edit: Your original question isn't clear either. If you can, edit it to something else? After all your question is about multi-jump not working - not about mistakes in typing or something.
     
    kpman2300 likes this.
  10. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    Yes I'll do it now!
     
  11. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    left.PNG Whenever I turn left my character disappears, it is still there but you can't see it. I tried flip option in sprite renderer but still nothing happened. what should I do?
     
  12. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Don't set
    transform.eulerAngles
    directly (frankly, I don't even know why the Unity team allowed it to be set in the first place).
    All rotation operations in Unity are done with quaternions, and
    transform.eulerAngles
    is just meant to be a conversion from a quaternion rotation to a Euler rotation, which is easier to read, but setting the value will cause weird results, and is probably why your sprite is disappearing.

    For setting a rotation, use Quaternion.Euler, which converts a Euler rotation value into a Quaternion:
    Code (CSharp):
    1. if(moveInput > 0)
    2. {
    3.    transform.rotation = Quaternion.Euler(Vector3.zero);
    4. }
    5. else if (moveInput < 0)
    6. {
    7.    transform.rotation = Quaternion.Euler(0f, 180f, 0f);
    8. }
     
    kpman2300 likes this.
  13. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    this didn't work, it's still happening!
    I tried something new, I set Z axis scale to 0 and it worked!
     
    Last edited: Oct 22, 2020
  14. kpman2300

    kpman2300

    Joined:
    Oct 5, 2020
    Posts:
    32
    Now, I am having a new problem, When I am turning my character, it does turn but it changes its position because it has a messed up pivot point and I am not able to bring it in center. My player is made by joining different parts.