178 lines
4.4 KiB
C#
178 lines
4.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Events;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
public CharacterController2D controller;
|
|
public AudioClip runClip;
|
|
public AudioClip jumpClip;
|
|
public AudioClip knockClip;
|
|
public ParticleSystem jumpParticles;
|
|
|
|
float horizontalMove = 0f;
|
|
public float runSpeed = 40f;
|
|
bool jump = false;
|
|
PlayerControls controls;
|
|
AudioSource runSource;
|
|
AudioSource jumpSource;
|
|
AudioSource knockSource;
|
|
|
|
void Awake()
|
|
{
|
|
controls = new PlayerControls();
|
|
controls.Gameplay.Jump.performed += OnJump;
|
|
controls.Gameplay.Use.performed += ctx => StartCoroutine(sceneLoader());
|
|
|
|
controls.Gameplay.MoveLeft.performed += MoveLeft_performed;
|
|
//controls.Gameplay.MoveLeft.canceled += ctx => runSpeed = 0;
|
|
controls.Gameplay.MoveRight.performed += MoveRight_performed;
|
|
//controls.Gameplay.MoveRight.canceled += ctx => runSpeed = 0;
|
|
|
|
if (runClip != null)
|
|
{
|
|
runSource = gameObject.AddComponent<AudioSource>();
|
|
runSource.clip = runClip;
|
|
runSource.loop = true;
|
|
runSource.playOnAwake = false;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Missing run audio clip");
|
|
}
|
|
|
|
if (jumpClip != null)
|
|
{
|
|
jumpSource = gameObject.AddComponent<AudioSource>();
|
|
jumpSource.clip = jumpClip;
|
|
jumpSource.loop = false;
|
|
jumpSource.playOnAwake = false;
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Missing jump audio clip");
|
|
}
|
|
|
|
if (knockClip != null)
|
|
{
|
|
knockSource = gameObject.AddComponent<AudioSource>();
|
|
knockSource.clip = knockClip;
|
|
knockSource.loop = false;
|
|
knockSource.playOnAwake = false;
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Missing knock audio clip");
|
|
}
|
|
}
|
|
|
|
IEnumerator sceneLoader()
|
|
{
|
|
knockSource.Play();
|
|
Debug.Log("Waiting for knock knock to stop playing...");
|
|
yield return new WaitUntil(() => !knockSource.isPlaying);
|
|
Debug.Log("Switching to inside scene");
|
|
SceneManager.LoadScene("Inside Scene");
|
|
}
|
|
|
|
private void MoveRight_performed(InputAction.CallbackContext ctx)
|
|
{
|
|
var input = ctx.ReadValue<float>();
|
|
horizontalMove = input * runSpeed;
|
|
if (input > 0.1)
|
|
{
|
|
if (!runSource.isPlaying)
|
|
{
|
|
runSource.Play();
|
|
}
|
|
}
|
|
else if (runSource.isPlaying)
|
|
{
|
|
runSource.Stop();
|
|
}
|
|
}
|
|
|
|
private void MoveLeft_performed(InputAction.CallbackContext ctx)
|
|
{
|
|
var input = ctx.ReadValue<float>();
|
|
horizontalMove = input * -runSpeed;
|
|
if (input > 0.1)
|
|
{
|
|
if (!runSource.isPlaying)
|
|
{
|
|
runSource.Play();
|
|
}
|
|
}
|
|
else if (runSource.isPlaying)
|
|
{
|
|
runSource.Stop();
|
|
}
|
|
}
|
|
|
|
void OnJump(InputAction.CallbackContext context)
|
|
{
|
|
Debug.LogWarning("OnJump()");
|
|
jump = true;
|
|
jumpSource.Play();
|
|
var emitter = jumpParticles.emission;
|
|
emitter.enabled = true;
|
|
//jumpParticles.Play();
|
|
//jumpParticles.Emit(100);
|
|
}
|
|
|
|
public void OnLanding()
|
|
{
|
|
Debug.LogWarning("OnLanding()");
|
|
var emitter = jumpParticles.emission;
|
|
emitter.enabled = false;
|
|
//jumpParticles.Stop();
|
|
}
|
|
|
|
void OnMoveLeft(InputAction.CallbackContext context)
|
|
{
|
|
//horizontalMove = context.
|
|
}
|
|
|
|
void OnMoveRight(InputAction.CallbackContext context)
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
// Read inputs here
|
|
void Update()
|
|
{
|
|
/*
|
|
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
|
|
|
|
if (Input.GetButtonDown("Jump"))
|
|
{
|
|
jump = true;
|
|
}
|
|
*/
|
|
}
|
|
|
|
// Is called at a fixed frequency.
|
|
// Do all physics stuff here
|
|
void FixedUpdate()
|
|
{
|
|
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
|
|
jump = false;
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
controls.Gameplay.Enable();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
controls.Gameplay.Disable();
|
|
}
|
|
}
|