C#:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
private Rigidbody2D body;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public float jumpForce;
public LayerMask whatisGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(moveInput * speed, body.velocity.y);
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatisGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
body.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
body.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= jumpTime.deltaTime;
} else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
}
I'm beginner and trying to have an object jump in Unity. But the deltaTime comes back with a CS1061 error and have had trouble finding any prev thread that explains what I'm doing wrong. Any help is appreciated.
Last edited by a moderator: