Hello,
In the Combat and Traversal Unity course, lecture 26, my console is not logging anything when I hit tab. I checked and all the input code seems to be correct.
Thanks!
public class Targeter : MonoBehaviour
{
private List<Target> targets = new List<Target>();
public Target CurrentTarget {get; private set;}
private void OnTriggerEnter(Collider other)
{
if(!other.TryGetComponent<Target>(out Target target)) {return;}
{
targets.Add(target);
}
}
void OnTriggerExit(Collider other)
{
if(!other.TryGetComponent<Target>(out Target target)) {return;}
{
targets.Remove(target);
}
}
public bool SelectTarget()
{
if (targets.Count == 0) {return false;}
CurrentTarget = targets[0];
return true;
}
public void Cancel()
{
CurrentTarget = null;
}
}
""name"": """",
""id"": ""29c53d39-e200-4351-8878-a53903c8a867"",
""path"": ""<Keyboard>/tab"",
""interactions"": """",
""processors"": """",
""groups"": ""Mouse and Keyboard"",
""action"": ""Target"",
""isComposite"": false,
""isPartOfComposite"": false
Let’s start with seeing if the Targetter is working in the first place:
Add these Debugs to OnTriggerEnter and OnTriggerExit inside the if statements
Debug.Log($"OnTriggerEnter adding {target.name} to targets");
Debug.Log($"OnTriggerExit removing {target.name} from targets");
Paste in your PlayerFreeLookState and InputHandler scripts and I’ll see if I can spot any issues there.
public class PlayerFreeLookState : PlayerBaseState
{
private const float AnimatorDampTime = 0.1f;
private readonly int FreeLookHash = Animator.StringToHash("FreeLookSpeed");
public PlayerFreeLookState(PlayerStateMachine stateMachine) : base(stateMachine)
{
}
public override void Enter()
{
stateMachine.InputReader.TargetEvent += OnTarget;
}
private void OnTarget()
{
if(stateMachine.Targeter.SelectTarget()) {return;}
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
public override void Tick(float deltaTime)
{
Vector3 movement = CalculateMovement();
stateMachine.controller.Move(movement * stateMachine.FreeLookmovementSpeed * deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero)
{
stateMachine.Animator.SetFloat(FreeLookHash, 0, AnimatorDampTime, deltaTime);
return;
}
stateMachine.Animator.SetFloat(FreeLookHash, 1, AnimatorDampTime, deltaTime);
FaceMovementDirection(movement, deltaTime);
//Debug.Log(stateMachine.InputReader.MovementValue);
}
public override void Exit()
{
stateMachine.InputReader.TargetEvent -= OnTarget;
}
private void OnJump()
{
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
private Vector3 CalculateMovement()
{
Vector3 forward = stateMachine.MainCameraTransform.forward;
Vector3 right = stateMachine.MainCameraTransform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
return forward * stateMachine.InputReader.MovementValue.y + right * stateMachine.InputReader.MovementValue.x;
}
private void FaceMovementDirection(Vector3 movement, float deltaTime)
{
stateMachine.transform.rotation = Quaternion.Lerp(stateMachine.transform.rotation, Quaternion.LookRotation(movement), deltaTime * stateMachine.RotationDampening);
}
}
public class InputReader : MonoBehaviour, Controls.IPlayerActions
{
public Vector2 MovementValue {get; private set;}
private Controls controls;
public event Action JumpEvent;
public event Action DodgeEvent;
public event Action TargetEvent;
public event Action CancelEvent;
void Start()
{
controls = new Controls();
controls.Player.SetCallbacks(this);
controls.Player.Enable();
}
private void OnDestroy()
{
controls.Player.Disable();
}
public void OnJump(InputAction.CallbackContext context)
{
if(!context.performed) { return; }
JumpEvent?.Invoke();
}
public void OnDodge(InputAction.CallbackContext context)
{
if(!context.performed) {return;}
DodgeEvent?.Invoke();
}
public void OnMove(InputAction.CallbackContext context)
{
MovementValue = context.ReadValue<Vector2>();
}
public void OnLook(InputAction.CallbackContext context)
{
}
public void OnTarget(InputAction.CallbackContext context)
{
if(!context.performed) {return;}
TargetEvent?.Invoke();
}
public void OnCancel(InputAction.CallbackContext context)
{
if(!context.performed) {return;}
CancelEvent?.Invoke();
}
}
Oh and the Triggers were not being logged after I added
The scripts look correct, so likely the issue is that we’re not detecting the Targets…
Make sure there is a Rigidbody set to IsKinematic on the Targeter with the collider and that the collider is set to IsTrigger.
Make sure all the enemies have a Target script on their root with the CharacterController.
They show up in the editor now! But now I get this error.
Let’s see the PlayerTargettingState.cs
public class PlayerTargetingState : PlayerBaseState
{
public PlayerTargetingState(PlayerStateMachine stateMachine) : base(stateMachine) { }
public override void Enter()
{
stateMachine.InputReader.CancelEvent += OnCancel;
}
public override void Tick(float deltaTime)
{
Debug.Log(stateMachine.Targeter.CurrentTarget.name);
}
public override void Exit()
{
stateMachine.InputReader.CancelEvent -= OnCancel;
}
public void OnCancel()
{
stateMachine.Targeter.Cancel();
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
}
Let’s modify the Tick() a bit to get some more Debug information…
public override void Tick(float deltaTime)
{
if(stateMachine.Targeter==null)
{
Debug.Log($"{stateMachine.name} does not have a Targetter assigned!"};
return;
}
if(stateMachine.Targeter.CurrentTarget==null)
{
Debug.Log($"Targeter has no target, returning to Freelook state");
OnCancel();
return;
}
Debug.Log($"Targetting {stateMachine.Targeter.CurrentTarget.name}");
}
It seems to be fixed. Does this look right?
I’m not sure if it’s okay but it seems like “Tab” only works when the target has left the trigger sphere.
Found it, in the Freelook state, you’re missing an ! in the check on the targetter for SelectTarget… essentially, it’s only entering when you don’t have a target.
system
Closed
August 25, 2022, 1:30am
13
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.