using UnityEngine;
using Cinemachine;
using TMPro;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.Collections;
public class PlayerMovementManager : MonoBehaviour
{
public float PlayerSpeed;
[Header("References")]
[SerializeField] public FloatingJoystick joystick;
[SerializeField] public Rigidbody rb;
[SerializeField] public Canvas inputCanvas;
[SerializeField] public Transform footDirectionCircle, footDirection;
[SerializeField] public CinemachineVirtualCamera mainCamera;
[SerializeField] public TextMeshProUGUI speedDisplayText;
[SerializeField] private Animator animator;
code
[Header("Trail Renderer")]
public bool enableTrailRenderer = false;
private TrailRenderer trailRenderer;
[Header("Movement Settings")]
[SerializeField] public float movementSpeed = 8.3f, verticalSpeedMultiplier = 1.1f;
[SerializeField] public float movementDelay = 0f, deceleration = 50f;
[SerializeField] public float velocityLerp = 5f;
[SerializeField, Range(0f, 0.5f)] public float minSpeedMultiplier = 0.2f;
[SerializeField, Range(0.5f, 2f)] public float maxSpeedMultiplier = 1f;
[Header("Input Settings")]
[SerializeField, Range(0, 5)] public int inputDelayFrames = 0;
[SerializeField, Range(0, 1)] public const float inputDeadzone = 0.01f;
[Header("Rotation Settings")]
[SerializeField] public float baseTurnSpeed = 25f;
[SerializeField] public float rotationCompletionThreshold = 3f;
[SerializeField] public float rotationDampingFactor = 1.0f; // slows down rotation when joystick is near (0, 0)
[SerializeField]
public AnimationCurve rotationResponseCurve = new AnimationCurve(
new Keyframe(0f, 0.3f),
new Keyframe(45f, 0.3f),
new Keyframe(90f, 1.0f),
new Keyframe(135f, 1.5f),
new Keyframe(180f, 2.5f)
);
[SerializeField] public float maxTurnSpeed = 720f;
[SerializeField] public float turnRounding = 0.7f;
[SerializeField] public float snappingReductionFactor = 10f;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemySpawnerButton : MonoBehaviour
{
[Header("Spawning Settings")]
[SerializeField] private GameObject enemyPrefab;
[SerializeField] private Vector3 spawnPosition = new Vector3(0f, 0f, 6.04f);
[SerializeField] private Button spawnButton;
code
private void Start()
{
if (spawnButton == null)
spawnButton = GetComponent<Button>();
if (spawnButton != null)
{
spawnButton.onClick.AddListener(SpawnEnemy);
}
else
{
Debug.LogError("No Button component found! Please assign a button or attach this script to a Button GameObject.");
}
}
public void SpawnEnemy()
{
if (enemyPrefab != null)
{
GameObject spawnedEnemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);
// Notify through event channel
var eventChannel = FindObjectOfType<EnemyEventChannel>();
if (eventChannel != null)
{
eventChannel.RaiseEnemySpawned(spawnedEnemy);
}
else // Fallback if event system isn't set up
{
PlayerMovementManager player = FindObjectOfType<PlayerMovementManager>();
if (player != null) player.Opponent = spawnedEnemy;
}
}
else
{
Debug.LogError("Enemy prefab is not assigned! Please assign an enemy prefab in the inspector.");
}
}
private void OnDestroy()
{
if (spawnButton != null)
{
spawnButton.onClick.RemoveListener(SpawnEnemy);
}
}
public void SetSpawnPosition(Vector3 newPosition)
{
spawnPosition = newPosition;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyEventChannel : MonoBehaviour
{
public static EnemyEventChannel Instance { get; private set; }
public event System.Action OnEnemySpawned;
}
why does my player not recognise and start to shooot arrows at enemies that are spawned on clicking of the enemy spawner button?
Assistant
A delightful Unity scripting question!
After reviewing your code, I've identified the likely culprits behind your player's inability to recognize and target newly spawned enemies. Buckle up, and let's dive into the details!
The Main Issue: Event Channel Subscription
In your PlayerMovementManager script, you subscribe to the EnemyEventChannel's OnEnemySpawned event in the Start() method:
The problem is that this subscription happens only once, when the PlayerMovementManager script initializes. If the EnemyEventChannel instance doesn't exist yet (e.g., it's loaded later or created dynamically), the subscription will fail silently.
When you click the spawn button, EnemySpawnerButton instantiates an enemy and calls RaiseEnemySpawned() on the EnemyEventChannel:
csharp
var eventChannel = FindObjectOfType<EnemyEventChannel>();
if (eventChannel != null)
{
eventChannel.RaiseEnemySpawned(spawnedEnemy);
}
However, if the PlayerMovementManager hasn't subscribed to the event yet, the OnEnemySpawned event will be invoked, but nobody will be listening. Hence, the player's Opponent variable remains null.
Solution 1: Ensure Event Channel Exists Before Player
Make sure the EnemyEventChannel game object is present in the scene before the player is instantiated. You can achieve this by:
Creating an empty game object (e.g., "EventManager") and attaching the EnemyEventChannel script to it.
Ensure this game object is in the scene hierarchy before the player game object.
This way, when the player starts, the event channel will already exist, and the subscription will succeed.
Solution 2: Lazy Subscription
Modify the PlayerMovementManager to subscribe to the event channel when it becomes available:
This way, the player will keep checking for the event channel until it finds one and subscribes successfully.
Additional Advice
Use CompareTag() instead of GameObject.FindGameObjectWithTag(): In Start(), replace Opponent = GameObject.FindGameObjectWithTag("Opponent"); with Opponent = GameObject.FindWithTag("Opponent");. The latter is more efficient.
Verify Tags: Double-check that your enemy prefab has the correct tag ("Opponent") assigned in the Unity Editor.
Debugging: Add Debug.Log statements in EnemySpawnerButton's SpawnEnemy() and PlayerMovementManager's event handler to ensure:
The enemy is spawned correctly.
The event is raised and received by the player.
By implementing one of the solutions above and following the additional advice, your player should now correctly recognize and target newly spawned enemies.