Search Unity

Question Why am my character snapping to one spot and not moving?

Discussion in '2D' started by Pro_154k, Aug 22, 2020.

  1. Pro_154k

    Pro_154k

    Joined:
    Aug 22, 2020
    Posts:
    1
    I had this work, but after my summer vacation when i came back the movement of my character was broken. I have troubleshot a bit and cant figure out what is wrong. I am new to unity.



    Script:




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

    public class Movement : MonoBehaviour



    {
    //Variables
    public float speed;
    public float jumpForce;
    bool isGrounded = false;
    public Transform isGroundedChecker;
    public float checkGroundRadius;
    public LayerMask groundLayer;
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    public float rememberGroundedFor;
    float lastTimeGrounded;
    bool fr = false;



    private Animator anim;

    private Transform tr;






    Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();

    tr = GetComponent<Transform>();

    anim = GetComponent<Animator>();


    }








    // Update is called once per frame
    void Update()
    {
    Move();
    Jump();
    CheckIfGrounded();
    BetterJump();
    }

    void Move()
    {
    float x = Input.GetAxisRaw("Horizontal");

    float moveBy = x * speed;

    rb.velocity = new Vector2(moveBy, rb.velocity.y);



    if (x == 0)
    {

    anim.SetBool("isRunning", false);

    }
    else
    {
    anim.SetBool("isRunning", true);
    Debug.Log("isrunning");
    }



    if(x > 0)
    {

    fr = true;
    }

    if(x < 0)
    {

    fr = false;
    }


    if(fr == true)
    {
    tr.rotation = new Quaternion(0, 1, 0, 0);

    }
    else
    {
    tr.rotation = new Quaternion(0, 0, 0, 0);


    }



    }




    void Jump()
    {
    if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor))
    {

    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    anim.SetTrigger("takeOff");
    }

    if (isGrounded == true)
    {
    anim.SetBool("isJumping", false);
    }

    else {
    anim.SetBool("isJumping", true);
    }

    }











    void CheckIfGrounded() {
    Collider2D colliders = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);

    if (colliders != null) {
    isGrounded = true;
    } else {
    if (isGrounded) {
    lastTimeGrounded = Time.time;
    }
    isGrounded = false;
    }
    }






    void BetterJump() {
    if (rb.velocity.y < 0) {

    rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;

    } else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space)) {
    rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
    }
    }

    }
     

    Attached Files:

    Last edited: Aug 23, 2020