cnam-geometry-dash/Assets/Scripts/PlayerScript.cs

91 lines
2.2 KiB
C#
Raw Normal View History

using UnityEngine;
public class PlayerScript : MonoBehaviour
{
public Rigidbody2D rigidBody;
public GameObject groundObject;
public Vector3 initialPosition;
2024-12-16 16:19:59 +01:00
public Quaternion initialRotation;
2024-12-16 16:19:59 +01:00
public ParticleSystem particle;
private bool wantsToJump = false;
public void Start()
{
initialPosition = transform.position;
2024-12-16 16:19:59 +01:00
initialRotation = transform.rotation;
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()
{
return Mathf.Abs(initialPosition.y - transform.position.y) > 0.05f;
}
private void AlignRotation()
{
Vector3 rotation = transform.rotation.eulerAngles;
rotation.z = Mathf.Round(rotation.z / 90) * 90;
transform.rotation = Quaternion.Euler(rotation);
}
private void UpdateParticlePositionAndRotation()
{
2024-12-16 16:19:59 +01:00
particle.transform.position = transform.position + new Vector3(-0.19f, -0.64f, 0);
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;
}
}