jump-out-of-the-world/Assets/Scripts/MovementController.cs

87 lines
2.4 KiB
C#
Raw Normal View History

2022-07-20 17:04:21 +08:00
/*
zlib License
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (C) 2022 Lucas
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1.The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
public bool isJumping = false;
private bool isPressing = false;
private bool isDoubleJumping = false;
private Rigidbody2D rb2D;
public float xSpeed;
public float jumpSpeed;
// Start is called before the first frame update
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
Vector2 speed = rb2D.velocity;
speed.x = xSpeed;
rb2D.velocity = speed;
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
Vector2 speed = rb2D.velocity;
speed.x = xSpeed;
if (Input.GetKey(KeyCode.Space) || Input.touchCount > 0)
{
if (!isPressing)
{
isPressing = true;
if (isJumping && !isDoubleJumping)
{
speed.y = jumpSpeed;
isDoubleJumping = true;
}
else if (!isJumping || !isDoubleJumping)
{
speed.y = jumpSpeed;
isJumping = true;
}
}
}
else
{
isPressing = false;
}
rb2D.velocity = speed;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
isDoubleJumping = false;
}
}
}