-
-
Save unity3dcollege/e7fa4cd9aa24d9cf25b33358994a7947 to your computer and use it in GitHub Desktop.
Player with Jump Velocity & Duration Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Player : MonoBehaviour | |
{ | |
private float _jumpEndTime; | |
[SerializeField] private float _jumpVelocity = 5; | |
[SerializeField] private float _jumpDuration = 0.5f; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
var horizontal = Input.GetAxis("Horizontal"); | |
Debug.Log(horizontal); | |
Rigidbody2D rb = GetComponent<Rigidbody2D>(); | |
var vertical = rb.velocity.y; | |
if (Input.GetButtonDown("Fire1")) | |
_jumpEndTime = Time.time + _jumpDuration; | |
if (Input.GetButton("Fire1") && _jumpEndTime > Time.time) | |
vertical = _jumpVelocity; | |
rb.velocity = new Vector2(horizontal, vertical); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment