103 lines
2.4 KiB
C#
Raw Normal View History

using UnityEngine;
2025-01-20 16:00:53 +01:00
using UnityEngine.SceneManagement;
public class PlayerScript : MonoBehaviour
{
public Rigidbody2D rigidBody;
2025-01-20 16:00:53 +01:00
public GameObject playerObject;
2024-12-16 16:19:59 +01:00
public ParticleSystem particle;
private bool wantsToJump = false;
2025-01-20 16:00:53 +01:00
public bool isColliding = true;
2025-01-06 16:11:40 +01:00
public AudioSource audioSource;
public void Start()
{
2024-12-16 16:19:59 +01:00
var mainModule = particle.main;
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
2024-12-16 16:19:59 +01:00
particle.transform.parent = null;
}
public void Update()
{
transform.position += Time.deltaTime * 8.6f * Vector3.right;
if (Input.GetKey(KeyCode.Space))
{
wantsToJump = true;
}
else
{
wantsToJump = false;
}
if (!IsJumping())
{
AlignRotation();
if (wantsToJump)
{
Jump();
wantsToJump = false;
}
2024-12-16 16:19:59 +01:00
particle.gameObject.SetActive(true);
}
else
{
2024-12-16 16:19:59 +01:00
particle.gameObject.SetActive(false);
transform.Rotate(Vector3.back * 360 * Time.deltaTime);
}
UpdateParticlePositionAndRotation();
UpdateParticleSystemSpeed();
}
private void Jump()
{
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0);
rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse);
}
private bool IsJumping()
{
2025-01-20 16:00:53 +01:00
return !isColliding;
}
private void AlignRotation()
{
Vector3 rotation = transform.rotation.eulerAngles;
rotation.z = Mathf.Round(rotation.z / 90) * 90;
transform.rotation = Quaternion.Euler(rotation);
}
private void UpdateParticlePositionAndRotation()
{
2025-01-06 16:11:40 +01:00
particle.transform.position = transform.position + new Vector3(-0.19f, -0.64f, -10);
2024-12-16 16:19:59 +01:00
particle.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
}
private void UpdateParticleSystemSpeed()
{
2024-12-16 16:19:59 +01:00
var velocityOverLifetime = particle.velocityOverLifetime;
velocityOverLifetime.x = rigidBody.linearVelocity.x;
}
2025-01-20 16:00:53 +01:00
public void OnCollisionEnter2D(Collision2D collision)
{
isColliding = true;
if (collision.gameObject.tag == "Kill")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
public void OnCollisionExit2D(Collision2D collision)
{
isColliding = false;
}
}