using DG.Tweening; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class Skywalker_PlayerController : MonoBehaviour { [Header("Movement Settings")] public float moveSpeed = 5f; public float jumpForce = 10f; [Header("Ground Check")] public Transform groundCheck; public float groundCheckRadius = 0.3f; public LayerMask groundLayer; [Header("Combat")] public GameObject Skywalker_BlueberryBlastPrefab; [Header("Animation")] public Animator animator; [Header("Mobile UI")] public Joystick joystick; // reference to joystick private bool jumpPressed = false; private bool shootPressed = false; private Rigidbody rb; private bool isGrounded; private float move; private float moveJoystick; private float moveEditor; private bool facingRight = true; public Transform shootPoint; void Start() { rb = GetComponent(); if (animator == null) animator = GetComponent(); } void Update() { Move(); Jump(); ShootBlast(); UpdateAnimations(); } void Move() { moveEditor = Input.GetAxis("Horizontal"); moveJoystick = joystick != null ? joystick.Horizontal : 0f; // get joystick x-axis if (moveJoystick != 0) { move = moveJoystick; } else if (moveEditor != 0) { move = moveEditor; } else { move = 0f; } Vector3 velocity = rb.velocity; velocity.x = move * moveSpeed; rb.velocity = velocity; if (move > 0 && !facingRight) { Flip(); } else if (move < 0 && facingRight) { Flip(); } } void Jump() { isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer); if(Input.GetKeyDown(KeyCode.Space)) jumpPressed = true; if (jumpPressed) { jumpPressed = false; // reset after using if (isGrounded) { rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z); animator.SetTrigger("Jump"); } } } public void ForceJumpForGameOver() { rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z); if (animator != null) animator.SetTrigger("Jump"); } void ShootBlast() { if (shootPressed && Skywalker_BlueberryBlastPrefab != null && shootPoint != null) { if (Skywalker_BlueberryManager.Instance.UseBlueberry()) { Instantiate(Skywalker_BlueberryBlastPrefab, shootPoint.position, shootPoint.rotation); } else { Debug.Log("No blueberries left!"); } shootPressed = false; // reset after firing } } void UpdateAnimations() { animator.SetFloat("Speed", Mathf.Abs(move)); animator.SetBool("IsGrounded", isGrounded); } void Flip() { facingRight = !facingRight; float targetY = facingRight ? 90f : 270f; transform.DORotate(new Vector3(0f, targetY, 0f), 0.1f, RotateMode.Fast); } void OnTriggerEnter(Collider col) { if (col.CompareTag("Blueberry")) { Destroy(col.gameObject); } if (col.CompareTag("Trap")) { Debug.Log("Hit Trap!"); } } void OnDrawGizmosSelected() { if (groundCheck != null) { Gizmos.color = Color.green; Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius); } } // --- Called by UI Buttons --- public void OnJumpButtonPressed() { jumpPressed = true; } public void OnShootButtonPressed() { shootPressed = true; } }