B
B
blobl2022-03-16 22:20:21
C++ / C#
blobl, 2022-03-16 22:20:21

It gives me an error when I want to run it in Unity. Who can help with the code?

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

public class Player : MonoBehaviour


{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Vector2 moveInput;
    private Vector2 moveVelocity;
    private Rigidbody2D rb;
    private bool facingRight = true;

    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    private Animator anim;


    void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }


    void Update()
    {
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce;
            anim.SetTrigger("takeOff");
        }

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

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



    }



    void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInput < 0)
        {
            Flip();
        }

        if (moveInput == 0)
        {
            anim.SetBool("isRunning", false);
        }

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


    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-03-16
@freeExec

Who to believe?

private float moveInput;

private Vector2 moveInput;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question