E
E
Eduardo752020-08-07 19:20:12
Unity
Eduardo75, 2020-08-07 19:20:12

Unity2D: How to fix jump issue?

New to Unity. I'm still starting to learn C #, I'm making a sample game by example. In this issue, after adding the private void Update() function, error cs1061 appeared:
float does not contain a definition for GetKeyDown and no available GetkeyDown extension method could be found that takes a first argument of type float. I haven't been able to find a solution yet. The code is below.

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

public class PlayerControler : MonoBehavior
{
public float speed;
public float jumpForce;
private float moveInput;

private Rigidbody2D rb;

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

private bool facingRight = true;

private void Start()
{
rb = GetComponent();
}

private 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();
}
}

private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGrounded);
if(isGrounded == true && moveInput.GetKeyDown(KeyCode.Space))
{
rb.velocity = Vector2.up * jumpForce;
}
}

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question