2024-12-16 11:01:50 +01:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
public Rigidbody2D rigidBody;
|
|
|
|
public GameObject groundObject;
|
|
|
|
|
|
|
|
public Vector3 initialPosition;
|
|
|
|
|
2024-12-16 11:59:21 +01:00
|
|
|
public ParticleSystem particleSystem;
|
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
private bool wantsToJump = false;
|
|
|
|
private bool isGrounded = true;
|
|
|
|
|
2024-12-16 11:01:50 +01:00
|
|
|
public void Start()
|
|
|
|
{
|
2024-12-16 14:55:46 +01:00
|
|
|
initialPosition = transform.position;
|
2024-12-16 11:59:21 +01:00
|
|
|
|
|
|
|
var mainModule = particleSystem.main;
|
|
|
|
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
|
|
|
|
|
|
particleSystem.transform.parent = null;
|
2024-12-16 11:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
transform.position += Time.deltaTime * 8.6f * Vector3.right;
|
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
if (Input.GetKey(KeyCode.Space))
|
|
|
|
{
|
|
|
|
wantsToJump = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wantsToJump = false;
|
|
|
|
}
|
|
|
|
|
2024-12-16 11:59:21 +01:00
|
|
|
if (!IsJumping())
|
2024-12-16 11:01:50 +01:00
|
|
|
{
|
2024-12-16 14:55:46 +01:00
|
|
|
isGrounded = true;
|
|
|
|
AlignRotation();
|
2024-12-16 11:59:21 +01:00
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
if (wantsToJump)
|
2024-12-16 11:01:50 +01:00
|
|
|
{
|
2024-12-16 14:55:46 +01:00
|
|
|
Jump();
|
|
|
|
wantsToJump = false;
|
2024-12-16 11:01:50 +01:00
|
|
|
}
|
2024-12-16 14:55:46 +01:00
|
|
|
|
2024-12-16 11:59:21 +01:00
|
|
|
particleSystem.gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-16 14:55:46 +01:00
|
|
|
isGrounded = false;
|
2024-12-16 11:59:21 +01:00
|
|
|
particleSystem.gameObject.SetActive(false);
|
|
|
|
transform.Rotate(Vector3.back * 360 * Time.deltaTime);
|
2024-12-16 11:01:50 +01:00
|
|
|
}
|
2024-12-16 11:59:21 +01:00
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
UpdateParticlePositionAndRotation();
|
|
|
|
UpdateParticleSystemSpeed();
|
|
|
|
}
|
2024-12-16 11:59:21 +01:00
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
private void Jump()
|
|
|
|
{
|
|
|
|
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0);
|
|
|
|
rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse);
|
2024-12-16 11:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsJumping()
|
|
|
|
{
|
|
|
|
return Mathf.Abs(initialPosition.y - transform.position.y) > 0.05f;
|
|
|
|
}
|
2024-12-16 11:59:21 +01:00
|
|
|
|
2024-12-16 14:55:46 +01:00
|
|
|
private void AlignRotation()
|
|
|
|
{
|
|
|
|
Vector3 rotation = transform.rotation.eulerAngles;
|
|
|
|
rotation.z = Mathf.Round(rotation.z / 90) * 90;
|
|
|
|
transform.rotation = Quaternion.Euler(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateParticlePositionAndRotation()
|
|
|
|
{
|
|
|
|
particleSystem.transform.position = transform.position + new Vector3(-0.19f, -0.64f, 0);
|
|
|
|
particleSystem.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateParticleSystemSpeed()
|
2024-12-16 11:59:21 +01:00
|
|
|
{
|
|
|
|
var velocityOverLifetime = particleSystem.velocityOverLifetime;
|
|
|
|
velocityOverLifetime.x = rigidBody.linearVelocity.x;
|
|
|
|
}
|
2024-12-16 11:01:50 +01:00
|
|
|
}
|