Add a double jump to this script. So that the player can click on Space one more time…
- User
Add a double jump to this script. So that the player can click on Space one more time when the character is in the air during the jump.
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))] public class MoveObject : MonoBehaviour { public float speed = 5f; public float jumpForce = 5f; public float gravity = 3.8f;
private Rigidbody2D rb; private Vector2 velocity; public bool isGrounded; public bool isJump; private int layerMask; void Start() { rb = GetComponent<Rigidbody2D>(); rb.gravityScale = gravity; // Применяем значение гравитации } void Update() { // Каст луча вниз из центра персонажа на небольшое расстояние RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.down), 0.2f, ~LayerMask.GetMask("Player")); if (hit) //Условие если рейкаст под ногами игрока задевает землю { if (Input.GetKeyDown(KeyCode.Space)) { Jump(); } } float horizontalInput = Input.GetAxis("Horizontal"); Vector2 movement = new Vector2(horizontalInput * speed, 0f); velocity.y -= gravity * Time.deltaTime; velocity.x = movement.x; velocity.y = rb.velocity.y; rb.velocity = new Vector2(velocity.x, velocity.y); } void Jump() { rb.velocity = new Vector2 (rb.velocity.x, jumpForce); }}
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.